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
namelist()returns a list of archive members by name;open(name)gives a binary file-like object.writestr(name, data)writes a str or bytes; a str is encoded as UTF-8 first.- The module does not handle multipart ZIP files.
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
- id: C1 claim: ZIP_DEFLATED requires the zlib module. source: S1 evidence: "This requires the zlib module."
- id: C2 claim: zipfile does not handle multipart ZIP files. source: S1 evidence: "This module does not handle multipart ZIP files."
- id: C3 claim: ZipFile reads and writes ZIP files. source: S2 evidence: "Class with methods to open, read, write, close, list zip files."
Sources
- [S1] zipfile - Work with ZIP archives. Python docs. https://docs.python.org/3/library/zipfile.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/zipfile/__init__.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/zipfile/__init__.py . License: PSF-2.0. Accessed 2026-11-18.
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
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.},
}
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.
- python-zipfile.table.json (table; CC-BY-4.0)
- python-zipfile.schema.json (schema; CC-BY-4.0)
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.