📄 zipfile-objects.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>7.16.1 ZipFile Objects </title>
<META NAME="description" CONTENT="7.16.1 ZipFile Objects ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="pyzipfile-objects.html" tppabs="http://www.python.org/doc/current/lib/pyzipfile-objects.html">
<LINK REL="previous" href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html">
<LINK REL="up" href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html">
<LINK REL="next" href="pyzipfile-objects.html" tppabs="http://www.python.org/doc/current/lib/pyzipfile-objects.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="pyzipfile-objects.html" tppabs="http://www.python.org/doc/current/lib/pyzipfile-objects.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html">7.16 zipfile </A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html">7.16 zipfile </A>
<b class="navlabel">Next:</b> <a class="sectref" href="pyzipfile-objects.html" tppabs="http://www.python.org/doc/current/lib/pyzipfile-objects.html">7.16.2 PyZipFile Objects</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H2>
<BR>
7.16.1 ZipFile Objects
</H2>
<P>
<dl><dt><b><a name='l2h-1763'><tt class='class'>ZipFile</tt></a></b> (<var>filename</var><big>[</big><var>, mode</var><big>[</big><var>, compression</var><big>]</big><big>]</big>)
<dd>
Open a ZIP file named <var>filename</var>. 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>filename</var>
refers to an existing ZIP file, then additional files are added to
it. If <var>filename</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
<P>
<dl><dd><pre class="verbatim">
cat myzip.zip >> python.exe
</pre></dl>
<P>
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" tppabs="http://www.python.org/doc/current/lib/module-zlib.html">zlib</a></tt> module is not avaialble,
<tt class="exception">RuntimeError</tt> is also raised. The default is
<tt class="constant">ZIP_STORED</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-1764'><tt class='method'>close</tt></a></b> ()
<dd>
Close the archive file. You must call <tt class="method">close()</tt> before
exiting your program or essential records will not be written.
</dl>
<P>
<dl><dt><b><a name='l2h-1765'><tt class='method'>getinfo</tt></a></b> (<var>name</var>)
<dd>
Return a <tt class="class">ZipInfo</tt> object with information about the archive
member <var>name</var>.
</dl>
<P>
<dl><dt><b><a name='l2h-1766'><tt class='method'>infolist</tt></a></b> ()
<dd>
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>
<P>
<dl><dt><b><a name='l2h-1767'><tt class='method'>namelist</tt></a></b> ()
<dd>
Return a list of archive members by name.
</dl>
<P>
<dl><dt><b><a name='l2h-1768'><tt class='method'>printdir</tt></a></b> ()
<dd>
Print a table of contents for the archive to <code>sys.stdout</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-1769'><tt class='method'>read</tt></a></b> (<var>name</var>)
<dd>
Return the bytes of the file in the archive. The archive must be
open for read or append.
</dl>
<P>
<dl><dt><b><a name='l2h-1770'><tt class='method'>testzip</tt></a></b> ()
<dd>
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>
<P>
<dl><dt><b><a name='l2h-1771'><tt class='method'>write</tt></a></b> (<var>filename</var><big>[</big><var>, arcname</var><big>[</big><var>,
compress_type</var><big>]</big><big>]</big>)
<dd>
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
<code>'a'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-1772'><tt class='method'>writestr</tt></a></b> (<var>zinfo, bytes</var>)
<dd>
Write the string <var>bytes</var> to the archive; meta-information is
given as the <tt class="class">ZipInfo</tt> instance <var>zinfo</var>. At least the
filename, date, and time must be given by <var>zinfo</var>. The archive
must be opened with mode <code>'w'</code> or <code>'a'</code>.
</dl>
<P>
The following data attribute is also available:
<P>
<dl><dt><b><a name='l2h-1773'><tt class='member'>debug</tt></a></b>
<dd>
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>.
</dl>
<P>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="pyzipfile-objects.html" tppabs="http://www.python.org/doc/current/lib/pyzipfile-objects.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html">7.16 zipfile </A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-zipfile.html" tppabs="http://www.python.org/doc/current/lib/module-zipfile.html">7.16 zipfile </A>
<b class="navlabel">Next:</b> <a class="sectref" href="pyzipfile-objects.html" tppabs="http://www.python.org/doc/current/lib/pyzipfile-objects.html">7.16.2 PyZipFile Objects</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -