Python zipfile module - Items and Objects

Python zipfile module - Items and Objects

The zipfile module does not currently handle multi-disk 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 the decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file. Decryption is extremely slow as it is implemented in native Python rather than C.

The module defines the following items:


exception zipfile.BadZipFile

The error raised for bad ZIP files.


exception zipfile.BadZipfile

Alias of BadZipFile, for compatibility with older Python versions.


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.


class zipfile.Path

A pathlib-compatible wrapper for zip files.

class zipfile.PyZipFile

Class for creating ZIP archives containing Python libraries.


class zipfile.ZipInfo

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.


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.

ZipFile Objects



Close the archive file. You must call close() before exiting your program or essential
records will not be written.


Return a
ZipInfo object with information about the archive member name. Calling getinfo() for a name not currently contained in the archive will raise a KeyError.


Return a list containing a ZipInfo object for each member of the archive. The objects are in the same order as their entries in the actual ZIP file on disk if an existing archive was opened., 
mode='r', pwd=None, *, force_zip64=False)

Access a member of the archive as a binary file-like object. name can be either the name of a file within the archive or a ZipInfo object. The mode parameter, if included, must be 'r' (the default) or 'w'. pwd is the password used to decrypt encrypted ZIP files.

path=None, pwd=None)

Extract a member from the archive to the current working directory; member must be its
full name or a ZipInfo object. Its file information is extracted as accurately
as possible.

members=None, pwd=None)

Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist(). 


Print a table of contents for the archive.


Set pwd as default password to extract encrypted files., pwd=None)

Return the bytes of the file name in the archive. name is the name of the file in the archive, or a ZipInfo object. The archive must be open for read or append. pwd is the password used for encrypted files and, if specified, it will override the default password set with setpassword().