← All notes

Python zipfile - create, read, append and safely extract ZIP archives

Answer: Open a zipfile.ZipFile(path, mode) in a with block: mode 'w' to write a new archive, 'r' to read, 'a' to append. The default compression=ZIP_STORED is the constant for an uncompressed archive member; ZIP_DEFLATED is the usual ZIP compression method.

Examples

import zipfile

with zipfile.ZipFile("bundle.zip", "w", compression=zipfile.ZIP_DEFLATED) as zf:
    zf.write("report.csv")
    zf.write("data/raw.json", arcname="raw.json")
    zf.writestr("README.txt", "generated by build.py\n")

with zipfile.ZipFile("bundle.zip") as zf:
    print(zf.namelist())
    with zf.open("README.txt") as f:
        print(f.read().decode())
    zf.extractall("out/")

zipfile.is_zipfile("bundle.zip")   # True
python -m zipfile -c monty.zip spam.txt eggs.txt
python -m zipfile -l monty.zip
python -m zipfile -e monty.zip target-dir/

Modes and compression

| Value | Use | |---|---| | 'r' / 'w' / 'a' / 'x' | read, truncate and write, append, or create exclusively | | ZIP_STORED | uncompressed archive member (the default) | | ZIP_DEFLATED | the usual ZIP compression method; requires zlib | | ZIP_BZIP2 / ZIP_LZMA | need the bz2 or lzma module |

ZIP64 extensions for files over 4 GiB are on by default (allowZip64=True).

Reading and writing

Safety

Never extract archives from untrusted sources without prior inspection. Members with absolute filenames or .. components could be created outside the target path; the module attempts to prevent that, but check namelist() first.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, zipfile - Work with ZIP archives; python/cpython (GitHub), Lib/zipfile/__init__.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, zipfile - Work with ZIP archives; python/cpython (GitHub), Lib/zipfile/__init__.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:mdsd3gc4

iinventedlight. 2026. "Python zipfile - create, read, append and safely extract ZIP archives." iinventedlight Library, iil:note:mdsd3gc4, version of 2026-11-18. https://iinventedlight.com/library/notes/python-zipfile/. License: CC-BY-4.0.

@misc{iil_note_mdsd3gc4,
  author = {{iinventedlight}},
  title = {{Python zipfile - create, read, append and safely extract ZIP archives}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/python-zipfile/},
  note = {Permanent ID iil:note:mdsd3gc4. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-zipfile.md.