Python Glossary Русский

all Python 3zipfileterm

zipfile

module

Reads and writes ZIP archives: listing members, extracting, adding files with compression.

Syntax

import zipfile

Use zipfile for .zip archives; .tar.gz has tarfile, and compressing a single stream without an archive is a job for gzip or zlib. By default ZipFile only stores files without compressing them: compression=zipfile.ZIP_DEFLATED turns it on. extractall() on an archive from an untrusted source is risky — member names can point outside the target directory.

Examples

import zipfile
with zipfile.ZipFile("demo.zip", "w", zipfile.ZIP_DEFLATED) as zf:
    zf.writestr("hello.txt", "hello zip")
with zipfile.ZipFile("demo.zip") as zf:
    print(zf.namelist(), zf.read("hello.txt").decode())  # → ['hello.txt'] hello zip

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy