Python ZipExtFile.close
Release a member stream while keeping its parent archive available.
A simple example
ZipExtFile.close
- Start with One open archive member
- Do this Close just that member
- Result Entry stream closed
Syntax
entry.close()
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyClose one receipt inside an archive
An archive reader finishes working with an entry or the surrounding archive. Release the appropriate handle at that boundary so processing later receipts does not accumulate open resources.
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:
with archive.open("tea.txt") as entry:
entry.read(1)
print(entry.closed)
True
The member is closed when its with block ends. The archive remains open until the outer block ends.
Inspect a choir archive
A teacher saves the bytes for the word alto in an uncompressed archive member. The reader finishes its work and closes the member stream. What is the state of that resource?
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. The reader finishes its work and closes the member stream. What is the state of that resource?
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.