/usr/share/doc/python3-docs/html/_sources/library
NameSizeModeActions
2to3.rst.txt158980644editdlrm
abc.rst.txt118050644editdlrm
aifc.rst.txt75030644editdlrm
allos.rst.txt6780644editdlrm
archiving.rst.txt4400644editdlrm
argparse.rst.txt774800644editdlrm
array.rst.txt108120644editdlrm
ast.rst.txt101590644editdlrm
asynchat.rst.txt85280644editdlrm
asyncio-dev.rst.txt133980644editdlrm
asyncio-eventloop.rst.txt337010644editdlrm
asyncio-eventloops.rst.txt76600644editdlrm
asyncio-protocol.rst.txt251700644editdlrm
asyncio-queue.rst.txt43280644editdlrm
asyncio-stream.rst.txt143630644editdlrm
asyncio-subprocess.rst.txt155440644editdlrm
asyncio-sync.rst.txt90780644editdlrm
asyncio-task.rst.txt254060644editdlrm
asyncio.rst.txt22720644editdlrm
asyncore.rst.txt136240644editdlrm
atexit.rst.txt36690644editdlrm
audioop.rst.txt107120644editdlrm
base64.rst.txt103710644editdlrm
bdb.rst.txt129850644editdlrm
binary.rst.txt6540644editdlrm
binascii.rst.txt65390644editdlrm
binhex.rst.txt17090644editdlrm
bisect.rst.txt53980644editdlrm
builtins.rst.txt14650644editdlrm
bz2.rst.txt88250644editdlrm
calendar.rst.txt109880644editdlrm
cgi.rst.txt226500644editdlrm
cgitb.rst.txt29180644editdlrm
chunk.rst.txt50940644editdlrm
cmath.rst.txt93260644editdlrm
cmd.rst.txt137950644editdlrm
code.rst.txt78170644editdlrm
codecs.rst.txt755640644editdlrm
codeop.rst.txt30210644editdlrm
collections.abc.rst.txt128610644editdlrm
collections.rst.txt460630644editdlrm
colorsys.rst.txt18200644editdlrm
compileall.rst.txt89750644editdlrm
concurrency.rst.txt6730644editdlrm
concurrent.futures.rst.txt169100644editdlrm
concurrent.rst.txt1710644editdlrm
configparser.rst.txt487560644editdlrm
constants.rst.txt33310644editdlrm
contextlib.rst.txt281750644editdlrm
copy.rst.txt33780644editdlrm
copyreg.rst.txt21820644editdlrm
crypt.rst.txt51220644editdlrm
crypto.rst.txt4110644editdlrm
csv.rst.txt199020644editdlrm
ctypes.rst.txt886130644editdlrm
curses.ascii.rst.txt89200644editdlrm
curses.panel.rst.txt27640644editdlrm
curses.rst.txt763970644editdlrm
custominterp.rst.txt5690644editdlrm
datatypes.rst.txt7510644editdlrm
datetime.rst.txt905240644editdlrm
dbm.rst.txt142930644editdlrm
debug.rst.txt4710644editdlrm
decimal.rst.txt748690644editdlrm
development.rst.txt7040644editdlrm
difflib.rst.txt298610644editdlrm
dis.rst.txt325670644editdlrm
distribution.rst.txt4520644editdlrm
distutils.rst.txt19740644editdlrm
doctest.rst.txt717430644editdlrm
dummy_threading.rst.txt7840644editdlrm
email.charset.rst.txt92930644editdlrm
email.compat32-message.rst.txt334930644editdlrm
email.contentmanager.rst.txt91120644editdlrm
email.encoders.rst.txt27150644editdlrm
email.errors.rst.txt47920644editdlrm
email.examples.rst.txt19150644editdlrm
email.generator.rst.txt136370644editdlrm
email.header.rst.txt91880644editdlrm
email.headerregistry.rst.txt179570644editdlrm
email.iterators.rst.txt27950644editdlrm
email.message.rst.txt330080644editdlrm
email.mime.rst.txt117210644editdlrm
email.parser.rst.txt140920644editdlrm
email.policy.rst.txt270220644editdlrm
email.rst.txt67850644editdlrm
email.util.rst.txt92140644editdlrm
ensurepip.rst.txt49870644editdlrm
enum.rst.txt327510644editdlrm
errno.rst.txt68110644editdlrm
exceptions.rst.txt255700644editdlrm
faulthandler.rst.txt61360644editdlrm
fcntl.rst.txt71520644editdlrm
filecmp.rst.txt56230644editdlrm
fileformats.rst.txt2870644editdlrm
fileinput.rst.txt80510644editdlrm
filesys.rst.txt9610644editdlrm
fnmatch.rst.txt31030644editdlrm
formatter.rst.txt132420644editdlrm
fpectl.rst.txt41800644editdlrm
fractions.rst.txt63640644editdlrm
frameworks.rst.txt3910644editdlrm
ftplib.rst.txt175520644editdlrm
functional.rst.txt3650644editdlrm
functions.rst.txt716210644editdlrm
functools.rst.txt185030644editdlrm
gc.rst.txt96940644editdlrm
getopt.rst.txt65570644editdlrm
getpass.rst.txt18820644editdlrm
gettext.rst.txt255880644editdlrm
glob.rst.txt35080644editdlrm
grp.rst.txt24170644editdlrm
gzip.rst.txt83010644editdlrm
hashlib.rst.txt265230644editdlrm
heapq.rst.txt134020644editdlrm
hmac.rst.txt40200644editdlrm
html.entities.rst.txt13190644editdlrm
html.parser.rst.txt112750644editdlrm
html.rst.txt13040644editdlrm
http.client.rst.txt188700644editdlrm
http.cookiejar.rst.txt278320644editdlrm
http.cookies.rst.txt86450644editdlrm
http.rst.txt70960644editdlrm
http.server.rst.txt176390644editdlrm
i18n.rst.txt4080644editdlrm
idle.rst.txt258340644editdlrm
imaplib.rst.txt203670644editdlrm
imghdr.rst.txt29380644editdlrm
imp.rst.txt155480644editdlrm
importlib.rst.txt535630644editdlrm
index.rst.txt22580644editdlrm
inspect.rst.txt539280644editdlrm
internet.rst.txt9920644editdlrm
intro.rst.txt26450644editdlrm
io.rst.txt403280644editdlrm
ipaddress.rst.txt323400644editdlrm
ipc.rst.txt6490644editdlrm
itertools.rst.txt384590644editdlrm
json.rst.txt278760644editdlrm
keyword.rst.txt6170644editdlrm
language.rst.txt5220644editdlrm
linecache.rst.txt24080644editdlrm
locale.rst.txt254700644editdlrm
logging.config.rst.txt338860644editdlrm
logging.handlers.rst.txt424970644editdlrm
logging.rst.txt604790644editdlrm
lzma.rst.txt173610644editdlrm
macpath.rst.txt7070644editdlrm
mailbox.rst.txt630550644editdlrm
mailcap.rst.txt36910644editdlrm
markup.rst.txt6790644editdlrm
marshal.rst.txt48550644editdlrm
math.rst.txt143750644editdlrm
mimetypes.rst.txt98390644editdlrm
misc.rst.txt2470644editdlrm
mm.rst.txt4310644editdlrm
mmap.rst.txt112320644editdlrm
modulefinder.rst.txt32400644editdlrm
modules.rst.txt3550644editdlrm
msilib.rst.txt186120644editdlrm
msvcrt.rst.txt43920644editdlrm
multiprocessing.rst.txt1044940644editdlrm
netdata.rst.txt3390644editdlrm
netrc.rst.txt30070644editdlrm
nis.rst.txt19930644editdlrm
nntplib.rst.txt218210644editdlrm
numbers.rst.txt80890644editdlrm
numeric.rst.txt6960644editdlrm
operator.rst.txt189890644editdlrm
optparse.rst.txt769960644editdlrm
os.path.rst.txt164190644editdlrm
os.rst.txt1375340644editdlrm
ossaudiodev.rst.txt178450644editdlrm
othergui.rst.txt28220644editdlrm
parser.rst.txt151480644editdlrm
pathlib.rst.txt300450644editdlrm
pdb.rst.txt194540644editdlrm
persistence.rst.txt5910644editdlrm
pickle.rst.txt374400644editdlrm
pickletools.rst.txt37290644editdlrm
pipes.rst.txt25570644editdlrm
pkgutil.rst.txt87300644editdlrm
platform.rst.txt96240644editdlrm
plistlib.rst.txt73980644editdlrm
poplib.rst.txt81970644editdlrm
posix.rst.txt36940644editdlrm
pprint.rst.txt142740644editdlrm
profile.rst.txt279690644editdlrm
pty.rst.txt31230644editdlrm
pwd.rst.txt27390644editdlrm
pyclbr.rst.txt32970644editdlrm
pydoc.rst.txt46880644editdlrm
pyexpat.rst.txt286200644editdlrm
python.rst.txt4750644editdlrm
py_compile.rst.txt38510644editdlrm
queue.rst.txt72680644editdlrm
quopri.rst.txt25710644editdlrm
random.rst.txt185030644editdlrm
re.rst.txt649360644editdlrm
readline.rst.txt119430644editdlrm
reprlib.rst.txt51620644editdlrm
resource.rst.txt122910644editdlrm
rlcompleter.rst.txt22940644editdlrm
runpy.rst.txt82850644editdlrm
sched.rst.txt48420644editdlrm
secrets.rst.txt59300644editdlrm
select.rst.txt281750644editdlrm
selectors.rst.txt89250644editdlrm
shelve.rst.txt83900644editdlrm
shlex.rst.txt164410644editdlrm
shutil.rst.txt260140644editdlrm
signal.rst.txt172180644editdlrm
site.rst.txt96280644editdlrm
smtpd.rst.txt108060644editdlrm
smtplib.rst.txt234390644editdlrm
sndhdr.rst.txt19940644editdlrm
socket.rst.txt682020644editdlrm
socketserver.rst.txt237120644editdlrm
spwd.rst.txt29580644editdlrm
sqlite3.rst.txt392150644editdlrm
ssl.rst.txt960210644editdlrm
stat.rst.txt98050644editdlrm
statistics.rst.txt153010644editdlrm
stdtypes.rst.txt1792400644editdlrm
string.rst.txt353740644editdlrm
stringprep.rst.txt42790644editdlrm
struct.rst.txt195810644editdlrm
subprocess.rst.txt457930644editdlrm
sunau.rst.txt73900644editdlrm
superseded.rst.txt2580644editdlrm
symbol.rst.txt9750644editdlrm
symtable.rst.txt49640644editdlrm
sys.rst.txt582730644editdlrm
sysconfig.rst.txt87490644editdlrm
syslog.rst.txt43020644editdlrm
tabnanny.rst.txt20070644editdlrm
tarfile.rst.txt317490644editdlrm
telnetlib.rst.txt78990644editdlrm
tempfile.rst.txt138570644editdlrm
termios.rst.txt37510644editdlrm
test.rst.txt257640644editdlrm
text.rst.txt5840644editdlrm
textwrap.rst.txt105190644editdlrm
threading.rst.txt392570644editdlrm
time.rst.txt329670644editdlrm
timeit.rst.txt130000644editdlrm
tk.rst.txt16390644editdlrm
tkinter.rst.txt330270644editdlrm
tkinter.scrolledtext.rst.txt12550644editdlrm
tkinter.tix.rst.txt226530644editdlrm
tkinter.ttk.rst.txt584890644editdlrm
token.rst.txt26070644editdlrm
tokenize.rst.txt100020644editdlrm
trace.rst.txt69140644editdlrm
traceback.rst.txt178540644editdlrm
tracemalloc.rst.txt220890644editdlrm
tty.rst.txt10970644editdlrm
turtle.rst.txt712210644editdlrm
types.rst.txt99990644editdlrm
typing.rst.txt343320644editdlrm
undoc.rst.txt7800644editdlrm
unicodedata.rst.txt57620644editdlrm
unittest.mock-examples.rst.txt462060644editdlrm
unittest.mock.rst.txt859910644editdlrm
unittest.rst.txt933040644editdlrm
unix.rst.txt4460644editdlrm
urllib.error.rst.txt21990644editdlrm
urllib.parse.rst.txt273320644editdlrm
urllib.request.rst.txt593600644editdlrm
urllib.robotparser.rst.txt29690644editdlrm
urllib.rst.txt4660644editdlrm
uu.rst.txt23870644editdlrm
uuid.rst.txt87600644editdlrm
venv.rst.txt206210644editdlrm
warnings.rst.txt201520644editdlrm
wave.rst.txt68700644editdlrm
weakref.rst.txt213280644editdlrm
webbrowser.rst.txt97830644editdlrm
windows.rst.txt2720644editdlrm
winreg.rst.txt240730644editdlrm
winsound.rst.txt51340644editdlrm
wsgiref.rst.txt330470644editdlrm
xdrlib.rst.txt80780644editdlrm
xml.dom.minidom.rst.txt101980644editdlrm
xml.dom.pulldom.rst.txt51860644editdlrm
xml.dom.rst.txt395070644editdlrm
xml.etree.elementtree.rst.txt438710644editdlrm
xml.rst.txt60710644editdlrm
xml.sax.handler.rst.txt153990644editdlrm
xml.sax.reader.rst.txt121330644editdlrm
xml.sax.rst.txt71590644editdlrm
xml.sax.utils.rst.txt39010644editdlrm
xmlrpc.client.rst.txt230710644editdlrm
xmlrpc.rst.txt4750644editdlrm
xmlrpc.server.rst.txt149900644editdlrm
zipapp.rst.txt173920644editdlrm
zipfile.rst.txt240050644editdlrm
zipimport.rst.txt58050644editdlrm
zlib.rst.txt139120644editdlrm
_dummy_thread.rst.txt7620644editdlrm
_thread.rst.txt68860644editdlrm
__future__.rst.txt52450644editdlrm
__main__.rst.txt9040644editdlrm
Edit: /usr/share/doc/python3-docs/html/_sources/library/zipfile.rst.txt (24005B)
:mod:`zipfile` --- Work with ZIP archives ========================================= .. module:: zipfile :synopsis: Read and write ZIP-format archive files. .. moduleauthor:: James C. Ahlstrom .. sectionauthor:: James C. Ahlstrom **Source code:** :source:`Lib/zipfile.py` -------------- The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in `PKZIP Application Note`_. This module does not currently handle multi-disk ZIP files. It can handle ZIP files that use the ZIP64 extensions (that is ZIP files that are more than 4 GiB in size). It supports decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file. Decryption is extremely slow as it is implemented in native Python rather than C. The module defines the following items: .. exception:: BadZipFile The error raised for bad ZIP files. .. versionadded:: 3.2 .. exception:: BadZipfile Alias of :exc:`BadZipFile`, for compatibility with older Python versions. .. deprecated:: 3.2 .. exception:: LargeZipFile The error raised when a ZIP file would require ZIP64 functionality but that has not been enabled. .. class:: ZipFile :noindex: The class for reading and writing ZIP files. See section :ref:`zipfile-objects` for constructor details. .. class:: PyZipFile :noindex: Class for creating ZIP archives containing Python libraries. .. class:: ZipInfo(filename='NoName', date_time=(1980,1,1,0,0,0)) Class used to represent information about a member of an archive. Instances of this class are returned by the :meth:`.getinfo` and :meth:`.infolist` methods of :class:`ZipFile` objects. Most users of the :mod:`zipfile` module will not need to create these, but only use those created by this module. *filename* should be the full name of the archive member, and *date_time* should be a tuple containing six fields which describe the time of the last modification to the file; the fields are described in section :ref:`zipinfo-objects`. .. function:: is_zipfile(filename) Returns ``True`` if *filename* is a valid ZIP file based on its magic number, otherwise returns ``False``. *filename* may be a file or file-like object too. .. versionchanged:: 3.1 Support for file and file-like objects. .. data:: ZIP_STORED The numeric constant for an uncompressed archive member. .. data:: ZIP_DEFLATED The numeric constant for the usual ZIP compression method. This requires the :mod:`zlib` module. .. data:: ZIP_BZIP2 The numeric constant for the BZIP2 compression method. This requires the :mod:`bz2` module. .. versionadded:: 3.3 .. data:: ZIP_LZMA The numeric constant for the LZMA compression method. This requires the :mod:`lzma` module. .. versionadded:: 3.3 .. note:: The ZIP file format specification has included support for bzip2 compression since 2001, and for LZMA compression since 2006. However, some tools (including older Python releases) do not support these compression methods, and may either refuse to process the ZIP file altogether, or fail to extract individual files. .. seealso:: `PKZIP Application Note`_ Documentation on the ZIP file format by Phil Katz, the creator of the format and algorithms used. `Info-ZIP Home Page `_ Information about the Info-ZIP project's ZIP archive programs and development libraries. .. _zipfile-objects: ZipFile Objects --------------- .. class:: ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True) Open a ZIP file, where *file* can be a path to a file (a string), a file-like object or a :term:`path-like object`. The *mode* parameter should be ``'r'`` to read an existing file, ``'w'`` to truncate and write a new file, ``'a'`` to append to an existing file, or ``'x'`` to exclusively create and write a new file. If *mode* is ``'x'`` and *file* refers to an existing file, a :exc:`FileExistsError` will be raised. If *mode* is ``'a'`` and *file* refers to an existing ZIP file, then additional files are added to it. If *file* does not refer to a ZIP file, then a new ZIP archive is appended to the file. This is meant for adding a ZIP archive to another file (such as :file:`python.exe`). If *mode* is ``'a'`` and the file does not exist at all, it is created. If *mode* is ``'r'`` or ``'a'``, the file should be seekable. *compression* is the ZIP compression method to use when writing the archive, and should be :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or :const:`ZIP_LZMA`; unrecognized values will cause :exc:`NotImplementedError` to be raised. If :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or :const:`ZIP_LZMA` is specified but the corresponding module (:mod:`zlib`, :mod:`bz2` or :mod:`lzma`) is not available, :exc:`RuntimeError` is raised. The default is :const:`ZIP_STORED`. If *allowZip64* is ``True`` (the default) zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 4 GiB. If it is false :mod:`zipfile` will raise an exception when the ZIP file would require ZIP64 extensions. If the file is created with mode ``'w'``, ``'x'`` or ``'a'`` and then :meth:`closed ` without adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file. ZipFile is also a context manager and therefore supports the :keyword:`with` statement. In the example, *myzip* is closed after the :keyword:`with` statement's suite is finished---even if an exception occurs:: with ZipFile('spam.zip', 'w') as myzip: myzip.write('eggs.txt') .. versionadded:: 3.2 Added the ability to use :class:`ZipFile` as a context manager. .. versionchanged:: 3.3 Added support for :mod:`bzip2 ` and :mod:`lzma` compression. .. versionchanged:: 3.4 ZIP64 extensions are enabled by default. .. versionchanged:: 3.5 Added support for writing to unseekable streams. Added support for the ``'x'`` mode. .. versionchanged:: 3.6 Previously, a plain :exc:`RuntimeError` was raised for unrecognized compression values. .. versionchanged:: 3.6.2 The *file* parameter accepts a :term:`path-like object`. .. method:: ZipFile.close() Close the archive file. You must call :meth:`close` before exiting your program or essential records will not be written. .. method:: ZipFile.getinfo(name) Return a :class:`ZipInfo` object with information about the archive member *name*. Calling :meth:`getinfo` for a name not currently contained in the archive will raise a :exc:`KeyError`. .. method:: ZipFile.infolist() Return a list containing a :class:`ZipInfo` object for each member of the archive. The objects are in the same order as their entries in the actual ZIP file on disk if an existing archive was opened. .. method:: ZipFile.namelist() Return a list of archive members by name. .. method:: ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False) Access a member of the archive as a binary file-like object. *name* can be either the name of a file within the archive or a :class:`ZipInfo` object. The *mode* parameter, if included, must be ``'r'`` (the default) or ``'w'``. *pwd* is the password used to decrypt encrypted ZIP files. :meth:`~ZipFile.open` is also a context manager and therefore supports the :keyword:`with` statement:: with ZipFile('spam.zip') as myzip: with myzip.open('eggs.txt') as myfile: print(myfile.read()) With *mode* ``'r'`` the file-like object (``ZipExtFile``) is read-only and provides the following methods: :meth:`~io.BufferedIOBase.read`, :meth:`~io.IOBase.readline`, :meth:`~io.IOBase.readlines`, :meth:`__iter__`, :meth:`~iterator.__next__`. These objects can operate independently of the ZipFile. With ``mode='w'``, a writable file handle is returned, which supports the :meth:`~io.BufferedIOBase.write` method. While a writable file handle is open, attempting to read or write other files in the ZIP file will raise a :exc:`ValueError`. When writing a file, if the file size is not known in advance but may exceed 2 GiB, pass ``force_zip64=True`` to ensure that the header format is capable of supporting large files. If the file size is known in advance, construct a :class:`ZipInfo` object with :attr:`~ZipInfo.file_size` set, and use that as the *name* parameter. .. note:: The :meth:`.open`, :meth:`read` and :meth:`extract` methods can take a filename or a :class:`ZipInfo` object. You will appreciate this when trying to read a ZIP file that contains members with duplicate names. .. versionchanged:: 3.6 Removed support of ``mode='U'``. Use :class:`io.TextIOWrapper` for reading compressed text files in :term:`universal newlines` mode. .. versionchanged:: 3.6 :meth:`open` can now be used to write files into the archive with the ``mode='w'`` option. .. versionchanged:: 3.6 Calling :meth:`.open` on a closed ZipFile will raise a :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. .. method:: ZipFile.extract(member, path=None, pwd=None) Extract a member from the archive to the current working directory; *member* must be its full name or a :class:`ZipInfo` object. Its file information is extracted as accurately as possible. *path* specifies a different directory to extract to. *member* can be a filename or a :class:`ZipInfo` object. *pwd* is the password used for encrypted files. Returns the normalized path created (a directory or new file). .. note:: If a member filename is an absolute path, a drive/UNC sharepoint and leading (back)slashes will be stripped, e.g.: ``///foo/bar`` becomes ``foo/bar`` on Unix, and ``C:\foo\bar`` becomes ``foo\bar`` on Windows. And all ``".."`` components in a member filename will be removed, e.g.: ``../../foo../../ba..r`` becomes ``foo../ba..r``. On Windows illegal characters (``:``, ``<``, ``>``, ``|``, ``"``, ``?``, and ``*``) replaced by underscore (``_``). .. versionchanged:: 3.6 Calling :meth:`extract` on a closed ZipFile will raise a :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. .. versionchanged:: 3.6.2 The *path* parameter accepts a :term:`path-like object`. .. method:: ZipFile.extractall(path=None, members=None, pwd=None) Extract all members from the archive to the current working directory. *path* specifies a different directory to extract to. *members* is optional and must be a subset of the list returned by :meth:`namelist`. *pwd* is the password used for encrypted files. .. warning:: Never extract archives from untrusted sources without prior inspection. It is possible that files are created outside of *path*, e.g. members that have absolute filenames starting with ``"/"`` or filenames with two dots ``".."``. This module attempts to prevent that. See :meth:`extract` note. .. versionchanged:: 3.6 Calling :meth:`extractall` on a closed ZipFile will raise a :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. .. versionchanged:: 3.6.2 The *path* parameter accepts a :term:`path-like object`. .. method:: ZipFile.printdir() Print a table of contents for the archive to ``sys.stdout``. .. method:: ZipFile.setpassword(pwd) Set *pwd* as default password to extract encrypted files. .. method:: ZipFile.read(name, pwd=None) Return the bytes of the file *name* in the archive. *name* is the name of the file in the archive, or a :class:`ZipInfo` object. The archive must be open for read or append. *pwd* is the password used for encrypted files and, if specified, it will override the default password set with :meth:`setpassword`. Calling :meth:`read` on a ZipFile that uses a compression method other than :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or :const:`ZIP_LZMA` will raise a :exc:`NotImplementedError`. An error will also be raised if the corresponding compression module is not available. .. versionchanged:: 3.6 Calling :meth:`read` on a closed ZipFile will raise a :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. .. method:: ZipFile.testzip() Read all the files in the archive and check their CRC's and file headers. Return the name of the first bad file, or else return ``None``. .. versionchanged:: 3.6 Calling :meth:`testfile` on a closed ZipFile will raise a :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. .. method:: ZipFile.write(filename, arcname=None, compress_type=None) Write the file named *filename* to the archive, giving it the archive name *arcname* (by default, this will be the same as *filename*, but without a drive letter and with leading path separators removed). If given, *compress_type* overrides the value given for the *compression* parameter to the constructor for the new entry. The archive must be open with mode ``'w'``, ``'x'`` or ``'a'``. .. note:: There is no official file name encoding for ZIP files. If you have unicode file names, you must convert them to byte strings in your desired encoding before passing them to :meth:`write`. WinZip interprets all file names as encoded in CP437, also known as DOS Latin. .. note:: Archive names should be relative to the archive root, that is, they should not start with a path separator. .. note:: If ``arcname`` (or ``filename``, if ``arcname`` is not given) contains a null byte, the name of the file in the archive will be truncated at the null byte. .. versionchanged:: 3.6 Calling :meth:`write` on a ZipFile created with mode ``'r'`` or a closed ZipFile will raise a :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. .. method:: ZipFile.writestr(zinfo_or_arcname, data[, compress_type]) Write the string *data* to the archive; *zinfo_or_arcname* is either the file name it will be given in the archive, or a :class:`ZipInfo` instance. If it's an instance, at least the filename, date, and time must be given. If it's a name, the date and time is set to the current date and time. The archive must be opened with mode ``'w'``, ``'x'`` or ``'a'``. If given, *compress_type* overrides the value given for the *compression* parameter to the constructor for the new entry, or in the *zinfo_or_arcname* (if that is a :class:`ZipInfo` instance). .. note:: When passing a :class:`ZipInfo` instance as the *zinfo_or_arcname* parameter, the compression method used will be that specified in the *compress_type* member of the given :class:`ZipInfo` instance. By default, the :class:`ZipInfo` constructor sets this member to :const:`ZIP_STORED`. .. versionchanged:: 3.2 The *compress_type* argument. .. versionchanged:: 3.6 Calling :meth:`writestr` on a ZipFile created with mode ``'r'`` or a closed ZipFile will raise a :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. The following data attributes are also available: .. attribute:: ZipFile.filename Name of the ZIP file. .. attribute:: ZipFile.debug The level of debug output to use. This may be set from ``0`` (the default, no output) to ``3`` (the most output). Debugging information is written to ``sys.stdout``. .. attribute:: ZipFile.comment The comment text associated with the ZIP file. If assigning a comment to a :class:`ZipFile` instance created with mode ``'w'``, ``'x'`` or ``'a'``, this should be a string no longer than 65535 bytes. Comments longer than this will be truncated in the written archive when :meth:`close` is called. .. _pyzipfile-objects: PyZipFile Objects ----------------- The :class:`PyZipFile` constructor takes the same parameters as the :class:`ZipFile` constructor, and one additional parameter, *optimize*. .. class:: PyZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, \ optimize=-1) .. versionadded:: 3.2 The *optimize* parameter. .. versionchanged:: 3.4 ZIP64 extensions are enabled by default. Instances have one method in addition to those of :class:`ZipFile` objects: .. method:: PyZipFile.writepy(pathname, basename='', filterfunc=None) Search for files :file:`\*.py` and add the corresponding file to the archive. If the *optimize* parameter to :class:`PyZipFile` was not given or ``-1``, the corresponding file is a :file:`\*.pyc` file, compiling if necessary. If the *optimize* parameter to :class:`PyZipFile` was ``0``, ``1`` or ``2``, only files with that optimization level (see :func:`compile`) are added to the archive, compiling if necessary. If *pathname* is a file, the filename must end with :file:`.py`, and just the (corresponding :file:`\*.pyc`) file is added at the top level (no path information). If *pathname* is a file that does not end with :file:`.py`, a :exc:`RuntimeError` will be raised. If it is a directory, and the directory is not a package directory, then all the files :file:`\*.pyc` are added at the top level. If the directory is a package directory, then all :file:`\*.pyc` are added under the package name as a file path, and if any subdirectories are package directories, all of these are added recursively. *basename* is intended for internal use only. *filterfunc*, if given, must be a function taking a single string argument. It will be passed each path (including each individual full file path) before it is added to the archive. If *filterfunc* returns a false value, the path will not be added, and if it is a directory its contents will be ignored. For example, if our test files are all either in ``test`` directories or start with the string ``test_``, we can use a *filterfunc* to exclude them:: >>> zf = PyZipFile('myprog.zip') >>> def notests(s): ... fn = os.path.basename(s) ... return (not (fn == 'test' or fn.startswith('test_'))) >>> zf.writepy('myprog', filterfunc=notests) The :meth:`writepy` method makes archives with file names like this:: string.pyc # Top level name test/__init__.pyc # Package directory test/testall.pyc # Module test.testall test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile .. versionadded:: 3.4 The *filterfunc* parameter. .. versionchanged:: 3.6.2 The *pathname* parameter accepts a :term:`path-like object`. .. _zipinfo-objects: ZipInfo Objects --------------- Instances of the :class:`ZipInfo` class are returned by the :meth:`.getinfo` and :meth:`.infolist` methods of :class:`ZipFile` objects. Each object stores information about a single member of the ZIP archive. There is one classmethod to make a :class:`ZipInfo` instance for a filesystem file: .. classmethod:: ZipInfo.from_file(filename, arcname=None) Construct a :class:`ZipInfo` instance for a file on the filesystem, in preparation for adding it to a zip file. *filename* should be the path to a file or directory on the filesystem. If *arcname* is specified, it is used as the name within the archive. If *arcname* is not specified, the name will be the same as *filename*, but with any drive letter and leading path separators removed. .. versionadded:: 3.6 .. versionchanged:: 3.6.2 The *filename* parameter accepts a :term:`path-like object`. Instances have the following methods and attributes: .. method:: ZipInfo.is_dir() Return ``True`` if this archive member is a directory. This uses the entry's name: directories should always end with ``/``. .. versionadded:: 3.6 .. attribute:: ZipInfo.filename Name of the file in the archive. .. attribute:: ZipInfo.date_time The time and date of the last modification to the archive member. This is a tuple of six values: +-------+--------------------------+ | Index | Value | +=======+==========================+ | ``0`` | Year (>= 1980) | +-------+--------------------------+ | ``1`` | Month (one-based) | +-------+--------------------------+ | ``2`` | Day of month (one-based) | +-------+--------------------------+ | ``3`` | Hours (zero-based) | +-------+--------------------------+ | ``4`` | Minutes (zero-based) | +-------+--------------------------+ | ``5`` | Seconds (zero-based) | +-------+--------------------------+ .. note:: The ZIP file format does not support timestamps before 1980. .. attribute:: ZipInfo.compress_type Type of compression for the archive member. .. attribute:: ZipInfo.comment Comment for the individual archive member. .. attribute:: ZipInfo.extra Expansion field data. The `PKZIP Application Note`_ contains some comments on the internal structure of the data contained in this string. .. attribute:: ZipInfo.create_system System which created ZIP archive. .. attribute:: ZipInfo.create_version PKZIP version which created ZIP archive. .. attribute:: ZipInfo.extract_version PKZIP version needed to extract archive. .. attribute:: ZipInfo.reserved Must be zero. .. attribute:: ZipInfo.flag_bits ZIP flag bits. .. attribute:: ZipInfo.volume Volume number of file header. .. attribute:: ZipInfo.internal_attr Internal attributes. .. attribute:: ZipInfo.external_attr External file attributes. .. attribute:: ZipInfo.header_offset Byte offset to the file header. .. attribute:: ZipInfo.CRC CRC-32 of the uncompressed file. .. attribute:: ZipInfo.compress_size Size of the compressed data. .. attribute:: ZipInfo.file_size Size of the uncompressed file. .. _zipfile-commandline: .. program:: zipfile Command-Line Interface ---------------------- The :mod:`zipfile` module provides a simple command-line interface to interact with ZIP archives. If you want to create a new ZIP archive, specify its name after the :option:`-c` option and then list the filename(s) that should be included: .. code-block:: shell-session $ python -m zipfile -c monty.zip spam.txt eggs.txt Passing a directory is also acceptable: .. code-block:: shell-session $ python -m zipfile -c monty.zip life-of-brian_1979/ If you want to extract a ZIP archive into the specified directory, use the :option:`-e` option: .. code-block:: shell-session $ python -m zipfile -e monty.zip target-dir/ For a list of the files in a ZIP archive, use the :option:`-l` option: .. code-block:: shell-session $ python -m zipfile -l monty.zip Command-line options ~~~~~~~~~~~~~~~~~~~~ .. cmdoption:: -l List files in a zipfile. .. cmdoption:: -c ... Create zipfile from source files. .. cmdoption:: -e Extract zipfile into target directory. .. cmdoption:: -t Test whether the zipfile is valid or not. .. _PKZIP Application Note: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT