/usr/share/doc/python2-docs/html/_sources/library
NameSizeModeActions
2to3.rst.txt146470644editdlrm
abc.rst.txt72190644editdlrm
aepack.rst.txt42570644editdlrm
aetools.rst.txt35320644editdlrm
aetypes.rst.txt42570644editdlrm
aifc.rst.txt70860644editdlrm
al.rst.txt53090644editdlrm
allos.rst.txt6950644editdlrm
anydbm.rst.txt41110644editdlrm
archiving.rst.txt4240644editdlrm
argparse.rst.txt746850644editdlrm
array.rst.txt105240644editdlrm
ast.rst.txt101110644editdlrm
asynchat.rst.txt92060644editdlrm
asyncore.rst.txt129370644editdlrm
atexit.rst.txt39100644editdlrm
audioop.rst.txt103950644editdlrm
autogil.rst.txt10150644editdlrm
base64.rst.txt62520644editdlrm
basehttpserver.rst.txt104040644editdlrm
bastion.rst.txt26110644editdlrm
bdb.rst.txt124560644editdlrm
binascii.rst.txt65080644editdlrm
binhex.rst.txt19100644editdlrm
bisect.rst.txt54150644editdlrm
bsddb.rst.txt75750644editdlrm
bz2.rst.txt80640644editdlrm
calendar.rst.txt112820644editdlrm
carbon.rst.txt159560644editdlrm
cd.rst.txt119740644editdlrm
cgi.rst.txt228530644editdlrm
cgihttpserver.rst.txt27880644editdlrm
cgitb.rst.txt28740644editdlrm
chunk.rst.txt49550644editdlrm
cmath.rst.txt76470644editdlrm
cmd.rst.txt85650644editdlrm
code.rst.txt71150644editdlrm
codecs.rst.txt669660644editdlrm
codeop.rst.txt37740644editdlrm
collections.rst.txt414480644editdlrm
colorpicker.rst.txt9130644editdlrm
colorsys.rst.txt18190644editdlrm
commands.rst.txt25950644editdlrm
compileall.rst.txt46780644editdlrm
compiler.rst.txt374640644editdlrm
configparser.rst.txt196070644editdlrm
constants.rst.txt23280644editdlrm
contextlib.rst.txt60120644editdlrm
cookie.rst.txt95430644editdlrm
cookielib.rst.txt278750644editdlrm
copy.rst.txt33510644editdlrm
copy_reg.rst.txt23290644editdlrm
crypt.rst.txt22920644editdlrm
crypto.rst.txt3550644editdlrm
csv.rst.txt227570644editdlrm
ctypes.rst.txt905130644editdlrm
curses.ascii.rst.txt90460644editdlrm
curses.panel.rst.txt27400644editdlrm
curses.rst.txt748770644editdlrm
custominterp.rst.txt5700644editdlrm
datatypes.rst.txt8640644editdlrm
datetime.rst.txt746790644editdlrm
dbhash.rst.txt38650644editdlrm
dbm.rst.txt31170644editdlrm
debug.rst.txt4460644editdlrm
decimal.rst.txt709260644editdlrm
development.rst.txt6400644editdlrm
difflib.rst.txt307160644editdlrm
dircache.rst.txt18130644editdlrm
dis.rst.txt232480644editdlrm
distribution.rst.txt4260644editdlrm
distutils.rst.txt19580644editdlrm
dl.rst.txt33920644editdlrm
doctest.rst.txt739850644editdlrm
docxmlrpcserver.rst.txt38020644editdlrm
dumbdbm.rst.txt28410644editdlrm
dummy_thread.rst.txt10580644editdlrm
dummy_threading.rst.txt7990644editdlrm
easydialogs.rst.txt103460644editdlrm
email-examples.rst.txt12710644editdlrm
email.charset.rst.txt96540644editdlrm
email.encoders.rst.txt23760644editdlrm
email.errors.rst.txt40060644editdlrm
email.generator.rst.txt61320644editdlrm
email.header.rst.txt75310644editdlrm
email.iterators.rst.txt24210644editdlrm
email.message.rst.txt252170644editdlrm
email.mime.rst.txt99170644editdlrm
email.parser.rst.txt103270644editdlrm
email.rst.txt161110644editdlrm
email.utils.rst.txt64750644editdlrm
ensurepip.rst.txt50220644editdlrm
errno.rst.txt67080644editdlrm
exceptions.rst.txt183430644editdlrm
fcntl.rst.txt73610644editdlrm
filecmp.rst.txt53480644editdlrm
fileformats.rst.txt3020644editdlrm
fileinput.rst.txt74170644editdlrm
filesys.rst.txt8060644editdlrm
fl.rst.txt176460644editdlrm
fm.rst.txt26990644editdlrm
fnmatch.rst.txt31040644editdlrm
formatter.rst.txt132450644editdlrm
fpectl.rst.txt41710644editdlrm
fpformat.rst.txt17470644editdlrm
fractions.rst.txt52970644editdlrm
framework.rst.txt114440644editdlrm
frameworks.rst.txt3780644editdlrm
ftplib.rst.txt157200644editdlrm
functions.rst.txt755540644editdlrm
functools.rst.txt74480644editdlrm
future_builtins.rst.txt20100644editdlrm
gc.rst.txt90140644editdlrm
gdbm.rst.txt48820644editdlrm
gensuitemodule.rst.txt31130644editdlrm
getopt.rst.txt66700644editdlrm
getpass.rst.txt18760644editdlrm
gettext.rst.txt290400644editdlrm
gl.rst.txt60090644editdlrm
glob.rst.txt24140644editdlrm
grp.rst.txt22560644editdlrm
gzip.rst.txt48280644editdlrm
hashlib.rst.txt73790644editdlrm
heapq.rst.txt131850644editdlrm
hmac.rst.txt30730644editdlrm
hotshot.rst.txt42890644editdlrm
htmllib.rst.txt73790644editdlrm
htmlparser.rst.txt116400644editdlrm
httplib.rst.txt374540644editdlrm
i18n.rst.txt4090644editdlrm
ic.rst.txt50050644editdlrm
idle.rst.txt221820644editdlrm
imageop.rst.txt40010644editdlrm
imaplib.rst.txt172210644editdlrm
imgfile.rst.txt27650644editdlrm
imghdr.rst.txt26350644editdlrm
imp.rst.txt125930644editdlrm
importlib.rst.txt11260644editdlrm
imputil.rst.txt70230644editdlrm
index.rst.txt22870644editdlrm
inspect.rst.txt281510644editdlrm
internet.rst.txt9500644editdlrm
intro.rst.txt28030644editdlrm
io.rst.txt390150644editdlrm
ipc.rst.txt6300644editdlrm
itertools.rst.txt363240644editdlrm
jpeg.rst.txt38580644editdlrm
json.rst.txt255450644editdlrm
keyword.rst.txt6170644editdlrm
language.rst.txt5230644editdlrm
linecache.rst.txt18870644editdlrm
locale.rst.txt249750644editdlrm
logging.config.rst.txt315600644editdlrm
logging.handlers.rst.txt281530644editdlrm
logging.rst.txt468820644editdlrm
mac.rst.txt7910644editdlrm
macos.rst.txt38240644editdlrm
macosa.rst.txt39640644editdlrm
macostools.rst.txt40170644editdlrm
macpath.rst.txt6500644editdlrm
mailbox.rst.txt681290644editdlrm
mailcap.rst.txt36750644editdlrm
markup.rst.txt12490644editdlrm
marshal.rst.txt56890644editdlrm
math.rst.txt109200644editdlrm
md5.rst.txt28150644editdlrm
mhlib.rst.txt39660644editdlrm
mimetools.rst.txt45040644editdlrm
mimetypes.rst.txt98390644editdlrm
mimewriter.rst.txt33600644editdlrm
mimify.rst.txt35190644editdlrm
miniaeframe.rst.txt25640644editdlrm
misc.rst.txt2480644editdlrm
mm.rst.txt4470644editdlrm
mmap.rst.txt104810644editdlrm
modulefinder.rst.txt33800644editdlrm
modules.rst.txt3820644editdlrm
msilib.rst.txt190620644editdlrm
msvcrt.rst.txt43460644editdlrm
multifile.rst.txt66130644editdlrm
multiprocessing.rst.txt926210644editdlrm
mutex.rst.txt19390644editdlrm
netdata.rst.txt4320644editdlrm
netrc.rst.txt31190644editdlrm
new.rst.txt26530644editdlrm
nis.rst.txt21080644editdlrm
nntplib.rst.txt145310644editdlrm
numbers.rst.txt80070644editdlrm
numeric.rst.txt7510644editdlrm
operator.rst.txt220920644editdlrm
optparse.rst.txt771010644editdlrm
os.path.rst.txt130940644editdlrm
os.rst.txt825800644editdlrm
ossaudiodev.rst.txt173090644editdlrm
othergui.rst.txt27210644editdlrm
parser.rst.txt153850644editdlrm
pdb.rst.txt160430644editdlrm
persistence.rst.txt8260644editdlrm
pickle.rst.txt372620644editdlrm
pickletools.rst.txt19970644editdlrm
pipes.rst.txt37860644editdlrm
pkgutil.rst.txt77140644editdlrm
platform.rst.txt95540644editdlrm
plistlib.rst.txt41340644editdlrm
popen2.rst.txt70220644editdlrm
poplib.rst.txt62200644editdlrm
posix.rst.txt36300644editdlrm
posixfile.rst.txt72000644editdlrm
pprint.rst.txt90710644editdlrm
profile.rst.txt286660644editdlrm
pty.rst.txt17620644editdlrm
pwd.rst.txt27250644editdlrm
pyclbr.rst.txt32960644editdlrm
pydoc.rst.txt40830644editdlrm
pyexpat.rst.txt288910644editdlrm
python.rst.txt5140644editdlrm
py_compile.rst.txt24790644editdlrm
queue.rst.txt70340644editdlrm
quopri.rst.txt26700644editdlrm
random.rst.txt133190644editdlrm
re.rst.txt551020644editdlrm
readline.rst.txt105130644editdlrm
repr.rst.txt47180644editdlrm
resource.rst.txt95950644editdlrm
restricted.rst.txt33270644editdlrm
rexec.rst.txt117420644editdlrm
rfc822.rst.txt140370644editdlrm
rlcompleter.rst.txt24940644editdlrm
robotparser.rst.txt21900644editdlrm
runpy.rst.txt69380644editdlrm
sched.rst.txt46440644editdlrm
scrolledtext.rst.txt13790644editdlrm
select.rst.txt207050644editdlrm
sets.rst.txt149730644editdlrm
sgi.rst.txt3220644editdlrm
sgmllib.rst.txt106640644editdlrm
sha.rst.txt28070644editdlrm
shelve.rst.txt83190644editdlrm
shlex.rst.txt113130644editdlrm
shutil.rst.txt134910644editdlrm
signal.rst.txt107190644editdlrm
simplehttpserver.rst.txt45590644editdlrm
simplexmlrpcserver.rst.txt108810644editdlrm
site.rst.txt78800644editdlrm
smtpd.rst.txt24640644editdlrm
smtplib.rst.txt149060644editdlrm
sndhdr.rst.txt17590644editdlrm
socket.rst.txt407650644editdlrm
socketserver.rst.txt225120644editdlrm
someos.rst.txt5990644editdlrm
spwd.rst.txt28250644editdlrm
sqlite3.rst.txt354480644editdlrm
ssl.rst.txt765540644editdlrm
stat.rst.txt77780644editdlrm
statvfs.rst.txt13000644editdlrm
stdtypes.rst.txt1226680644editdlrm
string.rst.txt440850644editdlrm
stringio.rst.txt41660644editdlrm
stringprep.rst.txt42380644editdlrm
strings.rst.txt7460644editdlrm
struct.rst.txt171000644editdlrm
subprocess.rst.txt336480644editdlrm
sun.rst.txt2490644editdlrm
sunau.rst.txt71240644editdlrm
sunaudio.rst.txt58490644editdlrm
symbol.rst.txt9750644editdlrm
symtable.rst.txt50640644editdlrm
sys.rst.txt475200644editdlrm
sysconfig.rst.txt76390644editdlrm
syslog.rst.txt39310644editdlrm
tabnanny.rst.txt19990644editdlrm
tarfile.rst.txt282140644editdlrm
telnetlib.rst.txt74820644editdlrm
tempfile.rst.txt105040644editdlrm
termios.rst.txt37330644editdlrm
test.rst.txt176590644editdlrm
textwrap.rst.txt85520644editdlrm
thread.rst.txt65880644editdlrm
threading.rst.txt324680644editdlrm
time.rst.txt260200644editdlrm
timeit.rst.txt115980644editdlrm
tix.rst.txt226930644editdlrm
tk.rst.txt16120644editdlrm
tkinter.rst.txt334560644editdlrm
token.rst.txt24510644editdlrm
tokenize.rst.txt55700644editdlrm
trace.rst.txt67240644editdlrm
traceback.rst.txt107110644editdlrm
ttk.rst.txt574110644editdlrm
tty.rst.txt10110644editdlrm
turtle.rst.txt640970644editdlrm
types.rst.txt62000644editdlrm
undoc.rst.txt65500644editdlrm
unicodedata.rst.txt57360644editdlrm
unittest.rst.txt829520644editdlrm
unix.rst.txt4900644editdlrm
urllib.rst.txt249600644editdlrm
urllib2.rst.txt352110644editdlrm
urlparse.rst.txt160710644editdlrm
user.rst.txt27480644editdlrm
userdict.rst.txt92900644editdlrm
uu.rst.txt23690644editdlrm
uuid.rst.txt83780644editdlrm
warnings.rst.txt198150644editdlrm
wave.rst.txt50470644editdlrm
weakref.rst.txt129260644editdlrm
webbrowser.rst.txt98630644editdlrm
whichdb.rst.txt9310644editdlrm
windows.rst.txt2730644editdlrm
winsound.rst.txt50660644editdlrm
wsgiref.rst.txt305670644editdlrm
xdrlib.rst.txt80770644editdlrm
xml.dom.minidom.rst.txt112080644editdlrm
xml.dom.pulldom.rst.txt15710644editdlrm
xml.dom.rst.txt401820644editdlrm
xml.etree.elementtree.rst.txt357070644editdlrm
xml.rst.txt60900644editdlrm
xml.sax.handler.rst.txt153710644editdlrm
xml.sax.reader.rst.txt122570644editdlrm
xml.sax.rst.txt64600644editdlrm
xml.sax.utils.rst.txt35600644editdlrm
xmlrpclib.rst.txt225590644editdlrm
zipfile.rst.txt188710644editdlrm
zipimport.rst.txt59570644editdlrm
zlib.rst.txt127660644editdlrm
_winreg.rst.txt233050644editdlrm
__builtin__.rst.txt14940644editdlrm
__future__.rst.txt49520644editdlrm
__main__.rst.txt5350644editdlrm
Edit: /usr/share/doc/python2-docs/html/_sources/library/zipfile.rst.txt (18871B)
:mod:`zipfile` --- Work with ZIP archives ========================================= .. module:: zipfile :synopsis: Read and write ZIP-format archive files. .. moduleauthor:: James C. Ahlstrom .. sectionauthor:: James C. Ahlstrom .. versionadded:: 1.6 **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 GByte 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 (old name: ``zipfile.error``). .. 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 Class for creating ZIP archives containing Python libraries. .. class:: ZipInfo([filename[, date_time]]) 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:: 2.7 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. No other compression methods are currently supported. .. 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[, compression[, allowZip64]]]) Open a ZIP file, where *file* can be either a path to a file (a string) or a file-like object. The *mode* parameter should be ``'r'`` to read an existing file, ``'w'`` to truncate and write a new file, or ``'a'`` to append to an existing file. 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`). .. versionchanged:: 2.6 If *mode* is ``a`` and the file does not exist at all, it is created. *compression* is the ZIP compression method to use when writing the archive, and should be :const:`ZIP_STORED` or :const:`ZIP_DEFLATED`; unrecognized values will cause :exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED` is specified but the :mod:`zlib` module is not available, :exc:`RuntimeError` is also raised. The default is :const:`ZIP_STORED`. If *allowZip64* is ``True`` zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 2 GB. If it is false (the default) :mod:`zipfile` will raise an exception when the ZIP file would require ZIP64 extensions. ZIP64 extensions are disabled by default because the default :program:`zip` and :program:`unzip` commands on Unix (the InfoZIP utilities) don't support these extensions. .. versionchanged:: 2.7.1 If the file is created with mode ``'a'`` or ``'w'`` 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:: 2.7 Added the ability to use :class:`ZipFile` as a context manager. .. 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. .. index:: single: universal newlines; zipfile.ZipFile.open method .. method:: ZipFile.open(name[, mode[, pwd]]) Extract a member from the archive as a file-like object (ZipExtFile). *name* is the name of the file in the archive, or a :class:`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 :term:`universal newline ` support in the read-only object. *pwd* is the password used for encrypted files. Calling :meth:`.open` on a closed ZipFile will raise a :exc:`RuntimeError`. .. note:: The file-like object is read-only and provides the following methods: :meth:`~file.read`, :meth:`~file.readline`, :meth:`~file.readlines`, :meth:`__iter__`, :meth:`~object.next`. .. note:: If the ZipFile was created by passing in a file-like object as the first argument to the constructor, then the object returned by :meth:`.open` shares the ZipFile's file pointer. Under these circumstances, the object returned by :meth:`.open` should not be used after any additional operations are performed on the ZipFile object. If the ZipFile was created by passing in a string (the filename) as the first argument to the constructor, then :meth:`.open` will create a new file object that will be held by the ZipExtFile, allowing it to operate independently of the ZipFile. .. 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. .. versionadded:: 2.6 .. method:: ZipFile.extract(member[, path[, pwd]]) 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). .. versionadded:: 2.6 .. 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 (``_``). .. method:: ZipFile.extractall([path[, members[, pwd]]]) 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 ``".."``. .. versionchanged:: 2.7.4 The zipfile module attempts to prevent that. See :meth:`extract` note. .. versionadded:: 2.6 .. 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. .. versionadded:: 2.6 .. method:: ZipFile.read(name[, pwd]) 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 closed ZipFile will raise a :exc:`RuntimeError`. .. versionchanged:: 2.6 *pwd* was added, and *name* can now be a :class:`ZipInfo` object. .. 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``. Calling :meth:`testzip` on a closed ZipFile will raise a :exc:`RuntimeError`. .. method:: ZipFile.write(filename[, arcname[, compress_type]]) 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'`` or ``'a'`` -- calling :meth:`write` on a ZipFile created with mode ``'r'`` will raise a :exc:`RuntimeError`. Calling :meth:`write` on a closed ZipFile will raise a :exc:`RuntimeError`. .. 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. .. method:: ZipFile.writestr(zinfo_or_arcname, bytes[, compress_type]) Write the string *bytes* 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'`` or ``'a'`` -- calling :meth:`writestr` on a ZipFile created with mode ``'r'`` will raise a :exc:`RuntimeError`. Calling :meth:`writestr` on a closed ZipFile will raise a :exc:`RuntimeError`. 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:: 2.7 The *compress_type* argument. The following data attributes are also available: .. 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 'a' or 'w', 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. Instances have one method in addition to those of :class:`ZipFile` objects. .. method:: PyZipFile.writepy(pathname[, basename]) Search for files :file:`\*.py` and add the corresponding file to the archive. The corresponding file is a :file:`\*.pyo` file if available, else a :file:`\*.pyc` file, compiling if necessary. If the pathname is a file, the filename must end with :file:`.py`, and just the (corresponding :file:`\*.py[co]`) file is added at the top level (no path information). If the 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:`\*.py[co]` are added at the top level. If the directory is a package directory, then all :file:`\*.py[co]` 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. The :meth:`writepy` method makes archives with file names like this:: string.pyc # Top level name test/__init__.pyc # Package directory test/test_support.pyc # Module test.test_support test/bogus/__init__.pyc # Subpackage directory test/bogus/myfile.pyc # Submodule test.bogus.myfile .. _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. Instances have the following attributes: .. 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