<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"pyzipfile-objects.html" />
<link rel=
"prev" href=
"module-zipfile.html" />
<link rel=
"parent" href=
"module-zipfile.html" />
<link rel=
"next" href=
"pyzipfile-objects.html" />
<meta name='aesop' content='information'
/>
<title>7.18.1 ZipFile Objects
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"7.18 zipfile "
href=
"module-zipfile.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"7.18 zipfile "
href=
"module-zipfile.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"7.18.2 PyZipFile Objects"
href=
"pyzipfile-objects.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-zipfile.html">7.18 zipfile
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-zipfile.html">7.18 zipfile
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"pyzipfile-objects.html">7.18.2 PyZipFile Objects
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION0091810000000000000000"></A><A NAME=
"zipfile-objects"></A>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
2858' xml:id='l2h-
2858'
class=
"class">ZipFile
</tt></b>(
</nobr></td>
<td><var>file
</var><big>[
</big><var>, mode
</var><big>[
</big><var>, compression
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Open a ZIP file, where
<var>file
</var> can be either a path to a file
(a string) or a file-like object. The
<var>mode
</var> parameter
should be
<code>'r'
</code> to read an existing file,
<code>'w'
</code> to
truncate and write a new file, or
<code>'a'
</code> to append to an
existing file. For
<var>mode
</var> is
<code>'a'
</code> and
<var>file
</var>
refers to an existing ZIP file, then additional files are added to
it. If
<var>file
</var> 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
<span class=
"file">python.exe
</span>. Using
<div class=
"verbatim"><pre>
cat myzip.zip
>> python.exe
also works, and at least
<b class=
"program">WinZip
</b> can read such files.
<var>compression
</var> is the ZIP compression method to use when writing
the archive, and should be
<tt class=
"constant">ZIP_STORED
</tt> or
<tt class=
"constant">ZIP_DEFLATED
</tt>; unrecognized values will cause
<tt class=
"exception">RuntimeError
</tt> to be raised. If
<tt class=
"constant">ZIP_DEFLATED
</tt>
is specified but the
<tt class=
"module"><a href=
"module-zlib.html">zlib
</a></tt> module is not available,
<tt class=
"exception">RuntimeError
</tt> is also raised. The default is
<tt class=
"constant">ZIP_STORED
</tt>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2859' xml:id='l2h-
2859'
class=
"method">close
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Close the archive file. You must call
<tt class=
"method">close()
</tt> before
exiting your program or essential records will not be written.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2860' xml:id='l2h-
2860'
class=
"method">getinfo
</tt></b>(
</nobr></td>
<td><var>name
</var>)
</td></tr></table></dt>
Return a
<tt class=
"class">ZipInfo
</tt> object with information about the archive
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2861' xml:id='l2h-
2861'
class=
"method">infolist
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Return a list containing a
<tt class=
"class">ZipInfo
</tt> 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.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2862' xml:id='l2h-
2862'
class=
"method">namelist
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Return a list of archive members by name.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2863' xml:id='l2h-
2863'
class=
"method">printdir
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Print a table of contents for the archive to
<code>sys.stdout
</code>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2864' xml:id='l2h-
2864'
class=
"method">read
</tt></b>(
</nobr></td>
<td><var>name
</var>)
</td></tr></table></dt>
Return the bytes of the file in the archive. The archive must be
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2865' xml:id='l2h-
2865'
class=
"method">testzip
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Read all the files in the archive and check their CRC's. Return the
name of the first bad file, or else return
<code>None
</code>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2866' xml:id='l2h-
2866'
class=
"method">write
</tt></b>(
</nobr></td>
<td><var>filename
</var><big>[
</big><var>, arcname
</var><big>[
</big><var>,
compress_type
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Write the file named
<var>filename
</var> to the archive, giving it the
archive name
<var>arcname
</var> (by default, this will be the same as
<var>filename
</var>). If given,
<var>compress_type
</var> overrides the value
given for the
<var>compression
</var> parameter to the constructor for
the new entry. The archive must be open with mode
<code>'w'
</code> or
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
2867' xml:id='l2h-
2867'
class=
"method">writestr
</tt></b>(
</nobr></td>
<td><var>zinfo_or_arcname, bytes
</var>)
</td></tr></table></dt>
Write the string
<var>bytes
</var> to the archive;
<var>zinfo_or_arcname
</var>
is either the file name it will be given in the archive, or a
<tt class=
"class">ZipInfo
</tt> 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
<code>'w'
</code> or
<code>'a'
</code>.
The following data attribute is also available:
<dl><dt><b><tt id='l2h-
2868' xml:id='l2h-
2868'
class=
"member">debug
</tt></b></dt>
The level of debug output to use. This may be set from
<code>0</code>
(the default, no output) to
<code>3</code> (the most output). Debugging
information is written to
<code>sys.stdout
</code>.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"7.18 zipfile "
href=
"module-zipfile.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"7.18 zipfile "
href=
"module-zipfile.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"7.18.2 PyZipFile Objects"
href=
"pyzipfile-objects.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-zipfile.html">7.18 zipfile
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-zipfile.html">7.18 zipfile
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"pyzipfile-objects.html">7.18.2 PyZipFile Objects
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.