Python~module gzip use method, and introduction! Accessories python ~ OS module!!! __python

Source: Internet
Author: User
This module provides a few simple interfaces to compress and decompress files
Similar to the GNU Project's gzip and gunzip, data compression is derived from the support of the Zlib module.
Gzip.open (filename, mode= ' RB ', compresslevel=9, Encoding=none, Errors=none, Newline=none)
Opens a gzip-compressed gzip-formatted file and returns a file object:
The parameter filename can be a true filename (a str or bytes object), or a read-write file object that already exists.
Parameter mode uses the following binary: ' R ', ' RB ', ' a ', ' AB ', ' WB '
When manipulating text, use: ' RT, ' at ', ' wt '
The default is: ' RB '
Parameter compresslevel is a value of 0-9
How to read a compressed file
import gzip
with Gzip.open ('/home/joe/file.txt.gz ', ' RB ') as f:
    file_content = F.read ()
How to create a gzip file
import gzip
content = B "Lots of content"
with Gzip.open ('/home/joe/file.txt.gz ', ' WB ') as F:
    f.write (content)
How to gzip an existing file (convert existing files to gzip format)
import gzip
import Shutil with
open ('/home/joe/file.txt ', ' RB ') as f_in:
    with Gzip.open ('/home/joe/file.txt.gz ', ' WB ') as F_out:
        shutil.copyfileobj (f_in, F_out)
How to gzip compress a binary character
Import gzip
s_in = B "Lots of content here"
s_out = gzip.compress (s_in)
Compressing files using the Gzip module
>>> Import gzip #导入python gzip module, note name is all small

>>> 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 a compressed file, the name of the file is empty or can be, do not modify

Fileobj is the generated compressed file object, its path name, and so on.

Finally, the file is written to the gzip file, and then close the operation of the connection.

filename = ' d: ... ' Whatever, for example:' d:\\test\\sitemap.log.gz '

extracting files using the Gzip module

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

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

When used, note that the size of the function method should look carefully

If the gzip file is in this form: *.tar.gz, first compressed by the TAR command

And then by gzip compression needs to first extract the tar file, and then use the Gzip module to decompress

In fact, many web pages in order to improve the browser end user access speed, and search engine crawler crawl speed, are using gzip compression.


It's not clear to see here. or here.
OS module look here

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.