Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 406221c

Browse files
committed
Issue #22154: Add an example to show context management protocol support of ZipFile.open().
Patch by Mike Short.
2 parents c2ccce7 + ce77ee9 commit 406221c

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

Doc/library/zipfile.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ ZipFile Objects
210210
password used for encrypted files. Calling :meth:`.open` on a closed
211211
ZipFile will raise a :exc:`RuntimeError`.
212212

213+
:func:`~ZipFile.open` is also a context manager and therefore supports the
214+
:keyword:`with` statement::
215+
216+
with ZipFile('spam.zip') as myzip:
217+
with myzip.open('eggs.txt') as myfile:
218+
print(myfile.read())
219+
213220
.. note::
214221

215222
The file-like object is read-only and provides the following methods:

0 commit comments

Comments
 (0)