Skip to content
Python: Read a ZIP directory with zipfile.Path

Python: Read a ZIP directory with zipfile.Path

November 26, 2026

zipfile.Path gives an open ZIP archive a directory-style interface. Use / to select a member and iterdir() to inspect children while keeping the owning ZipFile open.

Tested on Linux with Python 3.12.14. Use Python 3.12 or newer. The example uses only the standard library and requires no network access.

Run the example

Save this as archive_paths.py in an empty working directory:

from io import BytesIO
from zipfile import Path, ZipFile

with BytesIO() as buffer:
    with ZipFile(buffer, "w") as archive:
        archive.writestr("reports/summary.txt", "rows=3\n")
        archive.writestr("notes.txt", "synthetic archive\n")
    buffer.seek(0)
    with ZipFile(buffer) as archive:
        root = Path(archive)
        reports = root / "reports"
        print("Directory:", reports.is_dir())
        print("Members:", sorted(child.name for child in reports.iterdir()))
        print("Summary:", (reports / "summary.txt").read_text(encoding="utf-8").strip())

Run it without -O so any assertions remain enabled:

python3 archive_paths.py

Captured output:

Directory: True
Members: ['summary.txt']
Summary: rows=3

The archive contains a member named reports/summary.txt; no explicit directory entry is needed for reports to appear as a directory. Reading the summary happens inside the archive’s context, before the underlying file is closed.

Limits and cleanup

The archive is synthetic and stays in memory; nothing is extracted. Keep derived paths within the lifetime of their open archive. read_text() reads the member into memory, so this example is not a size-limited reader or a validator for untrusted archives.

After testing, remove the example script from its working directory:

rm -- archive_paths.py

Reference

Python 3.12 documentation

Acknowledgements

This post was created with assistance from AI.

Last updated on