On this page
zipfile.PyZipFile
Prev: zipfile.Path | Up: zipfile | Next: zipfile.ZipFile
Rel: zipfile.BadZipFile, zipfile.BadZipfile, zipfile.error, zipfile.is_zipfile
Ref: docs.python.org — zipfile.PyZipFile
Tags: #public #python3 #stdlib #zipfile
Import
import zipfile
from zipfile import PyZipFile
Signature & Overview
zipfile.PyZipFile(file, mode='r', compression=0, allowZip64=True, optimize=-1)
Class to create ZIP archives with Python library files and packages.
Methods & Attributes
PyZipFile.close(self)— Close the file, and for mode 'w', 'x' and 'a' write the ending records.PyZipFile.comment(...)— The comment text associated with the ZIP file.PyZipFile.extract(self, member, path=None, pwd=None)— Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. 'member' may be a filename or a ZipInfo object. You canPyZipFile.extractall(self, path=None, members=None, pwd=None)— Extract all members from the archive to the current working directory. 'path' specifies a different directory to extract to.PyZipFile.getinfo(self, name)— Return the instance of ZipInfo given 'name'.PyZipFile.infolist(self)— Return a list of class ZipInfo instances for files in the archive.PyZipFile.mkdir(self, zinfo_or_directory_name, mode=511)— Creates a directory inside the zip archive.PyZipFile.namelist(self)— Return a list of file names in the archive.PyZipFile.open(self, name, mode='r', pwd=None, *, force_zip64=False)— Return file-like object for 'name'.PyZipFile.printdir(self, file=None)— Print a table of contents for the zip file.PyZipFile.read(self, name, pwd=None)— Return file bytes for name. 'pwd' is the password to decrypt encrypted files.PyZipFile.setpassword(self, pwd)— Set default password for encrypted files.PyZipFile.testzip(self)— Read all the files and check the CRC.PyZipFile.write(self, filename, arcname=None, compress_type=None, compresslevel=None)— Put the bytes from filename into the archive under the name arcname.PyZipFile.writepy(self, pathname, basename='', filterfunc=None)— Add all files from "pathname" to the ZIP archive.PyZipFile.writestr(self, zinfo_or_arcname, data, compress_type=None, compresslevel=None)— Write a file into the archive. The contents is 'data', which may be either a 'str' or a 'bytes' instance; if it is a 'str', it is encoded as UTF-8 first.
Usage
import io
import zipfile
buf = io.BytesIO()
with zipfile.PyZipFile(buf, "w") as zf:
zf.writestr("mod.py", "X = 1\n")
assert zipfile.is_zipfile(buf)
Gotchas
- Specialized
ZipFilefor Python libraries:writepy()automatically compiles.pyfiles to.pycinside the archive forzipimport.
REPL Inspect
from zipfile import PyZipFile
help(PyZipFile)
Linked from
Related notes
- tuts/python3/stdlib_module/zipfile/Path Linked here; links to this page; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/ZipFile Linked here; links to this page; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/BadZipFile Linked here; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/BadZipfile_class Linked here; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/error Linked here; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/is_zipfile Linked here; shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipfile/LargeZipFile shared tags: #stdlib, #zipfile
- tuts/python3/stdlib_module/zipfile/ZipInfo shared tags: #stdlib, #zipfile
- tuts/c/stdlib shared tags: #stdlib