Python gzip Compression and decompression module

Source: Internet
Author: User

From  http://www.iplaypython.com/module/gzip.html
I. Compressing files using the Gzip module

>>> Import gzip #导入gzip模块, play Snake tips: Note the name is all lowercase

>>> g = gzip. Gzipfile (filename= "", mode= "WB", Compresslevel=9, Fileobj=open (' sitemap.log.gz ', ' WB '))

>>> g.write (Open (' D:\\test\\sitemap.xml '). Read ())

>>> G.close ()

Where the filename parameter is compressed within the file, the name of the file is empty and can not be modified. Fileobj is the resulting compressed file object, its path name, and so on. Finally, write the file to the gzip file, and then close the Operation connection.

Ii. using the Gzip module to unzip the file

>>> g = gzip. Gzipfile (mode= "RB", Fileobj=open (' d:\\test\\sitemap.log.gz ', ' RB '))

>>> Open (R "D:\\haha.xml", "WB"). Write (G.read ())

When using the note, the function method of the case to look carefully, if the gzip file is in this form: *.tar.gz, the proof is first compressed by the TAR command, and then by gzip compression, you need to unzip the tar file, and then use the Gzip module decompression. In fact, many web pages in order to improve browser-side user access speed, and search engine crawler crawl speed, are using gzip compression.

Python gzip Compression and decompression module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.