Python ZipInfo.filename
Look up a member record and read the name stored in the archive directory.
A simple example
ZipInfo.filename
- Start with An entry stored as notes/a.txt
- Do this Read its stored name
- Result notes/a.txt
Syntax
info.filename
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyRead an archived receipt name
An office keeps individual receipts inside one archive. Work with the archive and its named entries so a clerk can locate the intended receipt without treating the archive itself as a single receipt.
from io import BytesIO
from zipfile import ZipFile, ZIP_STORED
store = BytesIO()
with ZipFile(store, "w", compression=ZIP_STORED) as archive:
archive.writestr("tea.txt", "paid")
store.seek(0)
with ZipFile(store) as archive:
info = archive.getinfo("tea.txt")
print(info.filename)
tea.txt
A member name is relative to the archive and can contain directories.
Inspect a choir archive
A teacher saves the bytes for the word alto in an uncompressed archive member. Read the first entry name, retaining the complete folder prefix. Which archive path appears?
The archive contains one stored member. Its entry metadata and contents can be inspected independently.
Inspect a workshop archive
A tutor saves the bytes for glaze in a second uncompressed archive member. Read the first entry name, retaining the complete folder prefix. Which archive path appears?
The archive contains one stored member. Its entry metadata and contents can be inspected independently.
What to watch for
For an archive supplied by someone else, check expected member names and size limits before reading large payloads.