---
source: https://iinventedlight.com/library/notes/python-zipfile.md
title: "Python zipfile - create, read, append and safely extract ZIP archives"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: zipfile"
tags: [python, zipfile, archives, compression, files, stdlib]
client: Librarian
sources:
  - id: S1
    title: "zipfile - Work with ZIP archives"
    publisher: Python docs
    url: https://docs.python.org/3/library/zipfile.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/zipfile/__init__.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/zipfile/__init__.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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
```

```shell
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

- [python-pathlib](https://iinventedlight.com/library/notes/python-pathlib/)
- [python-read-write-files](https://iinventedlight.com/library/notes/python-read-write-files/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
