Python ZipFile.close
Close the ZIP archive after checking one of its members.
A simple example
ZipFile.close
- Start with An open archive
- Do this Close the archive
- Result Archive handle released
Syntax
archive.close()
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyFinish reading 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:
print(archive.getinfo("tea.txt").filename)
archive.close()
tea.txt
Closing releases resources held by the archive. A with block also closes it if reading fails.
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 archive handle. 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 archive handle. 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.