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