來源自 這裡
Preface :
This module provides a simple interface to compress and decompress files just like the GNU programs gzip and gunzip would. The data compression is provided by thezlib module. This module provides the GzipFile class which is modeled after Python’s File Object. The GzipFile class reads and writes gzip-format files, automatically compressing or decompressing the data so that it looks like an ordinary file object.
Note that additional file formats which can be decompressed by the gzip and gunzip programs, such as those produced by compress and pack, are not supported by this module. The module defines the following items :
- class gzip.GzipFile([filename[, mode[, compresslevel[, fileobj[, mtime]]]]])
- gzip.open(filename[, mode[, compresslevel]])
Examples of usage :
- Example of how to read a compressed file
- Example of how to create a compressed GZIP file
- Example of how to GZIP compress an existing file
- Example of read/write compressed file :
Preface :
This module provides a simple interface to compress and decompress files just like the GNU programs gzip and gunzip would. The data compression is provided by thezlib module. This module provides the GzipFile class which is modeled after Python’s File Object. The GzipFile class reads and writes gzip-format files, automatically compressing or decompressing the data so that it looks like an ordinary file object.
Note that additional file formats which can be decompressed by the gzip and gunzip programs, such as those produced by compress and pack, are not supported by this module. The module defines the following items :
- class gzip.GzipFile([filename[, mode[, compresslevel[, fileobj[, mtime]]]]])
- gzip.open(filename[, mode[, compresslevel]])
Examples of usage :
- Example of how to read a compressed file
- import gzip
- f = gzip.open('/home/joe/file.txt.gz', 'rb')
- file_content = f.read()
- f.close()
- import gzip
- content = "Lots of content here"
- f = gzip.open('/home/joe/file.txt.gz', 'wb')
- f.write(content)
- f.close()
- import gzip
- f_in = open('/home/joe/file.txt', 'rb')
- f_out = gzip.open('/home/joe/file.txt.gz', 'wb')
- f_out.writelines(f_in)
- f_out.close()
- f_in.close()
沒有留言:
張貼留言