zipfile.ZipFile.open()

ZipFile.open(name, mode='r', pwd=None)

Extract a member from the archive as a file-like object (ZipExtFile). name is the name of the file in the archive, or a ZipInfo object. The mode parameter, if included, must be one of the following: 'r' (the default), 'U', or 'rU'. Choosing 'U' or 'rU' will enable universal newlines support in the read-only object. pwd is the password used for encrypted files. Calling open() on a closed ZipFile will raise a RuntimeError.

open() is also a context manager and therefore supports the with statement:

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())

Note

The file-like object is read-only and provides the following methods: read(), readline(), readlines(), __iter__(), __next__().

Note

Objects returned by open() can operate independently of the ZipFile.

Note

The open(), read() and extract() methods can take a filename or a ZipInfo object. You will appreciate this when trying to read a ZIP file that contains members with duplicate names.

Deprecated since version 3.4, will be removed in version 3.6: The 'U' or 'rU' mode. Use io.TextIOWrapper for reading compressed text files in universal newlines mode.

doc_python
2016-10-07 17:48:41
Comments
Leave a Comment

Please login to continue.