Text of docs.python.org › pa › 3 › library › zipfile.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

zipfile --- Work with ZIP archives — Python 3.15.2 documentation

Navigation index modules | next | previous | Python » 3.15.2 documentation » The Python standard library » ਡਾਟਾ ਸੰਕੁਚਨ ਅਤੇ ਆਰਕਾਈਵਿੰਗ » zipfile --- Work with ZIP archives | Theme | zipfile --- Work with ZIP archives ¶ Source code: Lib/zipfile/ The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file.

Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note . This module does not handle multipart ZIP files. It can handle ZIP files that use the ZIP64 extensions (that is ZIP files that are more than 4 GiB in size). It supports decryption of encrypted files in ZIP archives, but it cannot create an encrypted file. Decryption is extremely slow as it is implemented in native Python rather than C.

Handling compressed archives requires optional modules such as zlib , bz2 , lzma , and compression.zstd . If any of them are missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see ਵਿਕਲਪਿਕ ਮੋਡੀਊਲਾਂ ਲਈ ਲੋੜਾਂ . The module defines the following items: exception zipfile. BadZipFile ¶ The error raised for bad ZIP files.

Added in version 3.2. exception zipfile. BadZipfile ¶ Alias of BadZipFile , for compatibility with older Python versions. Deprecated since version 3.2. exception zipfile. LargeZipFile ¶ The error raised when a ZIP file would require ZIP64 functionality but that has not been enabled. class zipfile. ZipFile The class for reading and writing ZIP files. See section ZipFile objects for constructor details. class zipfile.

Path Class that implements a subset of the interface provided by pathlib.Path , including the full importlib.resources.abc.Traversable interface. Added in version 3.8. class zipfile. PyZipFile Class for creating ZIP archives containing Python libraries. class zipfile. ZipInfo ( filename = 'NoName' , date_time = (1980, 1, 1, 0, 0, 0) ) ¶ Class used to represent information about a member of an archive. Instances of this class are returned by the getinfo() and infolist() methods of ZipFile objects.

Most users of the zipfile module will not need to create these, but only use those created by this module. filename should be the full name of the archive member, and date_time should be a tuple containing six fields which describe the time of the last modification to the file; the fields are described in section ZipInfo objects . Changed in version 3.13: A public compress_level attribute has been added to expose the formerly protected _compresslevel .

The older protected name continues to work as a property for backwards compatibility. _for_archive ( archive ) ¶ Resolve the date_time, compression attributes, and external attributes to suitable defaults as used by ZipFile.writestr() . Returns self for chaining. Added in version 3.14. zipfile. is_zipfile ( filename ) ¶ Returns True if filename is a valid ZIP file based on its magic number, otherwise returns False . filename may be a file or file-like object too.

Changed in version 3.1: Support for file and file-like objects. zipfile. ZIP_STORED ¶ The numeric constant for an uncompressed archive member. zipfile. ZIP_DEFLATED ¶ The numeric constant for the usual ZIP compression method. This requires the zlib module. zipfile. ZIP_BZIP2 ¶ The numeric constant for the BZIP2 compression method. This requires the bz2 module. Added in version 3.3. zipfile. ZIP_LZMA ¶ The numeric constant for the LZMA compression method.

This requires the lzma module. Added in version 3.3. zipfile. ZIP_ZSTANDARD ¶ The numeric constant for Zstandard compression. This requires the compression.zstd module. Note In APPNOTE 6.3.7, the method ID 20 was assigned to Zstandard compression. This was changed in APPNOTE 6.3.8 to method ID 93 to avoid conflicts, with method ID 20 being deprecated. For compatibility, the zipfile module reads both method IDs but will only write data with method ID 93 .

Added in version 3.14. Note The ZIP file format specification has included support for bzip2 compression since 2001, for LZMA compression since 2006, and Zstandard compression since 2020. However, some tools (including older Python releases) do not support these compression methods, and may either refuse to process the ZIP file altogether, or fail to extract individual files. See also PKZIP Application Note Documentation on the ZIP file format by Phil Katz, the creator of the format and algorithms used.

Info-ZIP Home Page Information about the Info-ZIP project's ZIP archive programs and development libraries. ZipFile objects ¶ class zipfile. ZipFile ( file , mode = 'r' , compression = ZIP_STORED , allowZip64 = True , compresslevel = None , * , strict_timestamps = True , metadata_encoding = None ) ¶ Open a ZIP file, where file can be a path to a file (a string), a file-like object or a path-like object . The mode parameter should be 'r' to read an existing file, 'w' to truncate and write a new file, 'a' to append to an existing file, or 'x' to exclusively create and write a new file.

If mode is 'x' and file refers to an existing file, a 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 python.exe ). If mode is 'a' and the file does not exist at all, it is created. If mode is 'r' or 'a' , the file should be seekable. compression is the ZIP compression method to use when writing the archive, and should be ZIP_STORED , ZIP_DEFLATED , ZIP_BZIP2 , ZIP_LZMA , or ZIP_ZSTANDARD ; unrecognized values will cause NotImplementedError to be raised.

If ZIP_DEFLATED , ZIP_BZIP2 , ZIP_LZMA , or ZIP_ZSTANDARD is specified but the corresponding module ( zlib , bz2 , lzma , or compression.zstd ) is not available, RuntimeError is raised. The default is ZIP_STORED . If allowZip64 is True (the default) zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 4 GiB. If it is false zipfile will raise an exception when the ZIP file would require ZIP64 extensions.

The compresslevel parameter controls the compression level to use when writing files to the archive. When using ZIP_STORED or ZIP_LZMA it has no effect. When using ZIP_DEFLATED integers 0 through 9 are accepted (see zlib for more information). When using ZIP_BZIP2 integers 1 through 9 are accepted (see bz2 for more information). When using ZIP_ZSTANDARD integers -131072 through 22 are commonly accepted (see CompressionParameter.compression_level for more on retrieving valid values and their meaning).

The strict_timestamps argument, when set to False , allows to zip files older than 1980-01-01 at the cost of setting the timestamp to 1980-01-01. Similar behavior occurs with files newer than 2107-12-31, the timestamp is also set to the limit. When mode is 'r' , metadata_encoding may be set to the name of a codec, which will be used to decode metadata such as the names of members and ZIP comments. If the file is created with mode 'w' , 'x' or 'a' and then closed without adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file.

ZipFile is also a context manager and therefore supports the with statement. In the example, myzip is closed after the with statement's suite is finished---even if an exception occurs: with ZipFile ( 'spam.zip' , 'w' ) as myzip : myzip . write ( 'eggs.txt' ) Note metadata_encoding is an instance-wide setting for the ZipFile. It is not possible to set this on a per-member basis. This attribute is a workaround for legacy implementations which produce archives with names in the current locale encoding or code page (mostly on Windows).

Acc

The rest of this page is on the live site.