diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2009-12-30 06:14:51 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2009-12-30 06:14:51 (GMT) |
commit | 569e61f35196cb765eb9940f75b27bb765e73073 (patch) | |
tree | 3bf8941cf86c15a9a6f9c4afbeb68211436e826b /Doc/library/zipfile.rst | |
parent | eb74da8e67bed4beef1bfc97384b3aa6e0f9836b (diff) | |
download | cpython-569e61f35196cb765eb9940f75b27bb765e73073.zip cpython-569e61f35196cb765eb9940f75b27bb765e73073.tar.gz cpython-569e61f35196cb765eb9940f75b27bb765e73073.tar.bz2 |
#5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Diffstat (limited to 'Doc/library/zipfile.rst')
-rw-r--r-- | Doc/library/zipfile.rst | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst index ba31b95..0d5f205 100644 --- a/Doc/library/zipfile.rst +++ b/Doc/library/zipfile.rst @@ -105,28 +105,35 @@ ZipFile Objects Open a ZIP file, where *file* can be either a path to a file (a string) or a file-like object. The *mode* parameter should be ``'r'`` to read an existing file, ``'w'`` to truncate and write a new file, or ``'a'`` to append to an - existing file. If *mode* is ``'a'`` and *file* refers to an existing ZIP file, - then additional files are added to it. If *file* does not refer to a ZIP file, - then a new ZIP archive is appended to the file. This is meant for adding a ZIP - archive to another file, such as :file:`python.exe`. Using :: - - cat myzip.zip >> python.exe - - also works, and at least :program:`WinZip` can read such files. If *mode* is - ``a`` and the file does not exist at all, it is created. *compression* is the - ZIP compression method to use when writing the archive, and should be - :const:`ZIP_STORED` or :const:`ZIP_DEFLATED`; unrecognized values will cause - :exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED` is specified but the - :mod:`zlib` module is not available, :exc:`RuntimeError` is also raised. The - default is :const:`ZIP_STORED`. If *allowZip64* is ``True`` zipfile will create - ZIP files that use the ZIP64 extensions when the zipfile is larger than 2 GB. If - it is false (the default) :mod:`zipfile` will raise an exception when the ZIP - file would require ZIP64 extensions. ZIP64 extensions are disabled by default - because the default :program:`zip` and :program:`unzip` commands on Unix (the - InfoZIP utilities) don't support these extensions. + existing file. If *mode* is ``'a'`` and *file* refers to an existing ZIP + file, then additional files are added to it. If *file* does not refer to a + ZIP file, then a new ZIP archive is appended to the file. This is meant for + adding a ZIP archive to another file (such as :file:`python.exe`). .. versionchanged:: 2.6 - If the file does not exist, it is created if the mode is 'a'. + If *mode* is ``a`` and the file does not exist at all, it is created. + + *compression* is the ZIP compression method to use when writing the archive, + and should be :const:`ZIP_STORED` or :const:`ZIP_DEFLATED`; unrecognized + values will cause :exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED` + is specified but the :mod:`zlib` module is not available, :exc:`RuntimeError` + is also raised. The default is :const:`ZIP_STORED`. If *allowZip64* is + ``True`` zipfile will create ZIP files that use the ZIP64 extensions when + the zipfile is larger than 2 GB. If it is false (the default) :mod:`zipfile` + will raise an exception when the ZIP file would require ZIP64 extensions. + ZIP64 extensions are disabled by default because the default :program:`zip` + and :program:`unzip` commands on Unix (the InfoZIP utilities) don't support + these extensions. + + ZipFile is also a context manager and therefore supports the + :keyword:`with` statement. In the example, *myzip* is closed after the + :keyword:`with` statement's suite is finished---even if an exception occurs:: + + with ZipFile('spam.zip', 'w') as myzip: + myzip.write('eggs.txt') + + .. versionadded:: 2.7 + Added the ability to use :class:`ZipFile` as a context manager. .. method:: ZipFile.close() |