diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-25 08:09:41 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-25 08:09:41 (GMT) |
commit | 764fc9bfac84d564c526628974a9bd5edfa5bcab (patch) | |
tree | 84a9d8736869ad41ca0c5e17550fb14cb9afba34 /Doc | |
parent | 489199765ff313d0a7dad70b8fbf416667c43862 (diff) | |
download | cpython-764fc9bfac84d564c526628974a9bd5edfa5bcab.zip cpython-764fc9bfac84d564c526628974a9bd5edfa5bcab.tar.gz cpython-764fc9bfac84d564c526628974a9bd5edfa5bcab.tar.bz2 |
Issue #21717: The zipfile.ZipFile.open function now supports 'x' (exclusive
creation) mode.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/zipfile.rst | 25 | ||||
-rw-r--r-- | Doc/whatsnew/3.5.rst | 3 |
2 files changed, 19 insertions, 9 deletions
diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst index c1dda25..a15c461 100644 --- a/Doc/library/zipfile.rst +++ b/Doc/library/zipfile.rst @@ -134,8 +134,11 @@ 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, ``'w'`` to truncate and write a new file, ``'x'`` to exclusive create + and write a new file, or ``'a'`` to append to an existing file. + If *mode* is ``'x'`` and *file* refers to an existing file, + a :exc:`FileExistsError` will be raised. + 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`). If @@ -152,7 +155,7 @@ ZipFile Objects extensions when the zipfile is larger than 2 GiB. If it is false :mod:`zipfile` will raise an exception when the ZIP file would require ZIP64 extensions. - If the file is created with mode ``'a'`` or ``'w'`` and then + If the file is created with mode ``'w'``, ``'x'`` or ``'a'`` and then :meth:`closed <close>` without adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file. @@ -174,6 +177,7 @@ ZipFile Objects .. versionchanged:: 3.5 Added support for writing to unseekable streams. + Added support for the ``'x'`` mode. .. method:: ZipFile.close() @@ -310,7 +314,8 @@ ZipFile Objects *arcname* (by default, this will be the same as *filename*, but without a drive letter and with leading path separators removed). If given, *compress_type* overrides the value given for the *compression* parameter to the constructor for - the new entry. The archive must be open with mode ``'w'`` or ``'a'`` -- calling + the new entry. + The archive must be open with mode ``'w'``, ``'x'`` or ``'a'`` -- calling :meth:`write` on a ZipFile created with mode ``'r'`` will raise a :exc:`RuntimeError`. Calling :meth:`write` on a closed ZipFile will raise a :exc:`RuntimeError`. @@ -337,10 +342,11 @@ ZipFile Objects Write the string *bytes* to the archive; *zinfo_or_arcname* is either the file name it will be given in the archive, or a :class:`ZipInfo` instance. If it's an instance, at least the filename, date, and time must be given. If it's a - name, the date and time is set to the current date and time. The archive must be - opened with mode ``'w'`` or ``'a'`` -- calling :meth:`writestr` on a ZipFile - created with mode ``'r'`` will raise a :exc:`RuntimeError`. Calling - :meth:`writestr` on a closed ZipFile will raise a :exc:`RuntimeError`. + name, the date and time is set to the current date and time. + The archive must be opened with mode ``'w'``, ``'x'`` or ``'a'`` -- calling + :meth:`writestr` on a ZipFile created with mode ``'r'`` will raise a + :exc:`RuntimeError`. Calling :meth:`writestr` on a closed ZipFile will + raise a :exc:`RuntimeError`. If given, *compress_type* overrides the value given for the *compression* parameter to the constructor for the new entry, or in the *zinfo_or_arcname* @@ -368,7 +374,8 @@ The following data attributes are also available: .. attribute:: ZipFile.comment The comment text associated with the ZIP file. If assigning a comment to a - :class:`ZipFile` instance created with mode 'a' or 'w', this should be a + :class:`ZipFile` instance created with mode ``'w'``, ``'x'`` or ``'a'``, + this should be a string no longer than 65535 bytes. Comments longer than this will be truncated in the written archive when :meth:`close` is called. diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst index aa5af04..0996350 100644 --- a/Doc/whatsnew/3.5.rst +++ b/Doc/whatsnew/3.5.rst @@ -454,6 +454,9 @@ zipfile * Added support for writing ZIP files to unseekable streams. (Contributed by Serhiy Storchaka in :issue:`23252`.) +* The :func:`zipfile.ZipFile.open` function now supports ``'x'`` (exclusive + creation) mode. (Contributed by Serhiy Storchaka in :issue:`21717`.) + Optimizations ============= |