Changeset 391 for python/trunk/Doc/library/gzip.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/gzip.rst
r2 r391 4 4 .. module:: gzip 5 5 :synopsis: Interfaces for gzip compression and decompression using file objects. 6 7 **Source code:** :source:`Lib/gzip.py` 8 9 -------------- 6 10 7 11 This module provides a simple interface to compress and decompress files just … … 19 23 :program:`compress` and :program:`pack`, are not supported by this module. 20 24 21 For other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and22 :mod:`tarfile` modules.23 24 25 The module defines the following items: 25 26 26 27 27 .. class:: GzipFile([filename[, mode[, compresslevel[, fileobj ]]]])28 .. class:: GzipFile([filename[, mode[, compresslevel[, fileobj[, mtime]]]]]) 28 29 29 30 Constructor for the :class:`GzipFile` class, which simulates most of the methods … … 33 34 34 35 The new class instance is based on *fileobj*, which can be a regular file, a 35 :class:` StringIO` object, or any other object which simulates a file. It36 :class:`~StringIO.StringIO` object, or any other object which simulates a file. It 36 37 defaults to ``None``, in which case *filename* is opened to provide a file 37 38 object. 38 39 39 40 When *fileobj* is not ``None``, the *filename* argument is only used to be 40 included in the :program:`gzip` file header, which may include sthe original41 included in the :program:`gzip` file header, which may include the original 41 42 filename of the uncompressed file. It defaults to the filename of *fileobj*, if 42 43 discernible; otherwise, it defaults to the empty string, and in this case the … … 49 50 in binary mode for cross-platform portability. 50 51 51 The *compresslevel* argument is an integer from ``1`` to ``9`` controlling the 52 level of compression; ``1`` is fastest and produces the least compression, and 53 ``9`` is slowest and produces the most compression. The default is ``9``. 52 The *compresslevel* argument is an integer from ``0`` to ``9`` controlling 53 the level of compression; ``1`` is fastest and produces the least 54 compression, and ``9`` is slowest and produces the most compression. ``0`` 55 is no compression. The default is ``9``. 56 57 The *mtime* argument is an optional numeric timestamp to be written to 58 the stream when compressing. All :program:`gzip` compressed streams are 59 required to contain a timestamp. If omitted or ``None``, the current 60 time is used. This module ignores the timestamp when decompressing; 61 however, some programs, such as :program:`gunzip`\ , make use of it. 62 The format of the timestamp is the same as that of the return value of 63 ``time.time()`` and of the ``st_mtime`` attribute of the object returned 64 by ``os.stat()``. 54 65 55 66 Calling a :class:`GzipFile` object's :meth:`close` method does not close 56 67 *fileobj*, since you might wish to append more material after the compressed 57 data. This also allows you to pass a :class:` StringIO` object opened for68 data. This also allows you to pass a :class:`~StringIO.StringIO` object opened for 58 69 writing as *fileobj*, and retrieve the resulting memory buffer using the 59 :class:`StringIO` object's :meth:`getvalue` method. 70 :class:`StringIO` object's :meth:`~StringIO.StringIO.getvalue` method. 71 72 :class:`GzipFile` supports iteration and the :keyword:`with` statement. 73 74 .. versionchanged:: 2.7 75 Support for the :keyword:`with` statement was added. 76 77 .. versionchanged:: 2.7 78 Support for zero-padded files was added. 79 80 .. versionadded:: 2.7 81 The *mtime* argument. 60 82 61 83 … … 75 97 76 98 import gzip 77 f = gzip.open(' /home/joe/file.txt.gz', 'rb')99 f = gzip.open('file.txt.gz', 'rb') 78 100 file_content = f.read() 79 101 f.close() … … 83 105 import gzip 84 106 content = "Lots of content here" 85 f = gzip.open(' /home/joe/file.txt.gz', 'wb')107 f = gzip.open('file.txt.gz', 'wb') 86 108 f.write(content) 87 109 f.close() … … 90 112 91 113 import gzip 92 f_in = open(' /home/joe/file.txt', 'rb')93 f_out = gzip.open(' /home/joe/file.txt.gz', 'wb')114 f_in = open('file.txt', 'rb') 115 f_out = gzip.open('file.txt.gz', 'wb') 94 116 f_out.writelines(f_in) 95 117 f_out.close()
Note:
See TracChangeset
for help on using the changeset viewer.