Python 3.8+zipfileterm
zipfile.Path
ZIP archives
A pathlib-style object for navigating a ZIP archive: the / operator, iterdir(), read_text(), exists().
Syntax
zipfile.Path(root, at='')
Handy when your code is already written for pathlib: the same / and read_text() work inside the archive without extracting. But it is not a pathlib.Path: nothing outside the archive exists for it, and constructing one checks nothing — a missing name only fails on read, so call exists() first. read_text() without encoding uses the default encoding just like open() (the locale encoding before 3.15); directories in a ZIP are often implicit, and Path infers them from name prefixes.
Examples
import io, zipfile
buf = io.BytesIO()
with zipfile.ZipFile(buf, 'w') as z:
z.writestr('data/config.txt', 'debug=1')
root = zipfile.Path(buf)
print([p.name for p in root.iterdir()]) # → ['data']
cfg = root / 'data' / 'config.txt'
print(cfg.read_text(encoding='utf-8')) # → debug=1
print((root / 'data' / 'nope.txt').exists()) # → False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy