zipfile.ZipFile

class zipfile.ZipFile(file, mode='r', compression=0, allowZip64=False)[source]

Class with methods to open, read, write, close, list zip files.

z = ZipFile(file, mode=”r”, compression=ZIP_STORED, allowZip64=False)

file: Either the path to the file, or a file-like object.
If it is a path, the file will be opened and closed by ZipFile.

mode: The mode can be either read “r”, write “w” or append “a”. compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib). allowZip64: if True ZipFile will create files with ZIP64 extensions when

needed, otherwise it will raise an exception when this would be necessary.

Methods

_RealGetContents() Read in the table of contents for the ZIP file.
__del__() Call the “close()” method in case the user forgot.
__enter__()
__exit__(type, value, traceback)
__format__ default object formatter
__init__(file[, mode, compression, allowZip64]) Open the ZIP file with mode read “r”, write “w” or append “a”.
__new__((S, ...)
__reduce__ helper for pickle
__reduce_ex__ helper for pickle
__sizeof__(() -> int) size of object in memory, in bytes
__subclasshook__ Abstract classes can override this to customize issubclass().
_extract_member(member, targetpath, pwd) Extract the ZipInfo object ‘member’ to a physical file on the path targetpath.
_writecheck(zinfo) Check for errors before writing a file to the archive.
close() Close the file, and for mode “w” and “a” write the ending records.
extract(member[, path, pwd]) Extract a member from the archive to the current working directory, using its full name.
extractall([path, members, pwd]) Extract all members from the archive to the current working directory.
getinfo(name) Return the instance of ZipInfo given ‘name’.
infolist() Return a list of class ZipInfo instances for files in the archive.
namelist() Return a list of file names in the archive.
open(name[, mode, pwd]) Return file-like object for ‘name’.
printdir() Print a table of contents for the zip file.
read(name[, pwd]) Return file bytes (as a string) for name.
setpassword(pwd) Set default password for encrypted files.
testzip() Read all the files and check the CRC.
write(filename[, arcname, compress_type]) Put the bytes from filename into the archive under the name arcname.
writestr(zinfo_or_arcname, bytes[, ...]) Write a file into the archive.

Attributes

comment The comment text associated with the ZIP file.
fp