gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1369 lines
126KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="zipfile — Work with ZIP archives" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/zipfile.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/zipfile/ 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 ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/zipfile/ 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 ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>zipfile — Work with ZIP archives &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="tarfile — Read and write tar archive files" href="tarfile.html" />
  33. <link rel="prev" title="lzma — Compression using the LZMA algorithm" href="lzma.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/zipfile.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> — Work with ZIP archives</a><ul>
  86. <li><a class="reference internal" href="#zipfile-objects">ZipFile Objects</a></li>
  87. <li><a class="reference internal" href="#path-objects">Path Objects</a></li>
  88. <li><a class="reference internal" href="#pyzipfile-objects">PyZipFile Objects</a></li>
  89. <li><a class="reference internal" href="#zipinfo-objects">ZipInfo Objects</a></li>
  90. <li><a class="reference internal" href="#command-line-interface">Command-Line Interface</a><ul>
  91. <li><a class="reference internal" href="#command-line-options">Command-line options</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#decompression-pitfalls">Decompression pitfalls</a><ul>
  95. <li><a class="reference internal" href="#from-file-itself">From file itself</a></li>
  96. <li><a class="reference internal" href="#file-system-limitations">File System limitations</a></li>
  97. <li><a class="reference internal" href="#resources-limitations">Resources limitations</a></li>
  98. <li><a class="reference internal" href="#interruption">Interruption</a></li>
  99. <li><a class="reference internal" href="#default-behaviors-of-extraction">Default behaviors of extraction</a></li>
  100. </ul>
  101. </li>
  102. </ul>
  103. </li>
  104. </ul>
  105. </div>
  106. <div>
  107. <h4>Previous topic</h4>
  108. <p class="topless"><a href="lzma.html"
  109. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code> — Compression using the LZMA algorithm</a></p>
  110. </div>
  111. <div>
  112. <h4>Next topic</h4>
  113. <p class="topless"><a href="tarfile.html"
  114. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code> — Read and write tar archive files</a></p>
  115. </div>
  116. <div role="note" aria-label="source link">
  117. <h3>This Page</h3>
  118. <ul class="this-page-menu">
  119. <li><a href="../bugs.html">Report a Bug</a></li>
  120. <li>
  121. <a href="https://github.com/python/cpython/blob/main/Doc/library/zipfile.rst"
  122. rel="nofollow">Show Source
  123. </a>
  124. </li>
  125. </ul>
  126. </div>
  127. </nav>
  128. </div>
  129. </div>
  130. <div class="related" role="navigation" aria-label="related navigation">
  131. <h3>Navigation</h3>
  132. <ul>
  133. <li class="right" style="margin-right: 10px">
  134. <a href="../genindex.html" title="General Index"
  135. accesskey="I">index</a></li>
  136. <li class="right" >
  137. <a href="../py-modindex.html" title="Python Module Index"
  138. >modules</a> |</li>
  139. <li class="right" >
  140. <a href="tarfile.html" title="tarfile — Read and write tar archive files"
  141. accesskey="N">next</a> |</li>
  142. <li class="right" >
  143. <a href="lzma.html" title="lzma — Compression using the LZMA algorithm"
  144. accesskey="P">previous</a> |</li>
  145. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  146. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  147. <li class="switchers">
  148. <div class="language_switcher_placeholder"></div>
  149. <div class="version_switcher_placeholder"></div>
  150. </li>
  151. <li>
  152. </li>
  153. <li id="cpython-language-and-version">
  154. <a href="../index.html">3.12.3 Documentation</a> &#187;
  155. </li>
  156. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  157. <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">Data Compression and Archiving</a> &#187;</li>
  158. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> — Work with ZIP archives</a></li>
  159. <li class="right">
  160. <div class="inline-search" role="search">
  161. <form class="inline-search" action="../search.html" method="get">
  162. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  163. <input type="submit" value="Go" />
  164. </form>
  165. </div>
  166. |
  167. </li>
  168. <li class="right">
  169. <label class="theme-selector-label">
  170. Theme
  171. <select class="theme-selector" oninput="activateTheme(this.value)">
  172. <option value="auto" selected>Auto</option>
  173. <option value="light">Light</option>
  174. <option value="dark">Dark</option>
  175. </select>
  176. </label> |</li>
  177. </ul>
  178. </div>
  179. <div class="document">
  180. <div class="documentwrapper">
  181. <div class="bodywrapper">
  182. <div class="body" role="main">
  183. <section id="module-zipfile">
  184. <span id="zipfile-work-with-zip-archives"></span><h1><a class="reference internal" href="#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> — Work with ZIP archives<a class="headerlink" href="#module-zipfile" title="Link to this heading">¶</a></h1>
  185. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/zipfile/">Lib/zipfile/</a></p>
  186. <hr class="docutils" />
  187. <p>The ZIP file format is a common archive and compression standard. This module
  188. provides tools to create, read, write, append, and list a ZIP file. Any
  189. advanced use of this module will require an understanding of the format, as
  190. defined in <a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a>.</p>
  191. <p>This module does not currently handle multi-disk ZIP files.
  192. It can handle ZIP files that use the ZIP64 extensions
  193. (that is ZIP files that are more than 4 GiB in size). It supports
  194. decryption of encrypted files in ZIP archives, but it currently cannot
  195. create an encrypted file. Decryption is extremely slow as it is
  196. implemented in native Python rather than C.</p>
  197. <p>The module defines the following items:</p>
  198. <dl class="py exception">
  199. <dt class="sig sig-object py" id="zipfile.BadZipFile">
  200. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">BadZipFile</span></span><a class="headerlink" href="#zipfile.BadZipFile" title="Link to this definition">¶</a></dt>
  201. <dd><p>The error raised for bad ZIP files.</p>
  202. <div class="versionadded">
  203. <p><span class="versionmodified added">New in version 3.2.</span></p>
  204. </div>
  205. </dd></dl>
  206. <dl class="py exception">
  207. <dt class="sig sig-object py" id="zipfile.BadZipfile">
  208. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">BadZipfile</span></span><a class="headerlink" href="#zipfile.BadZipfile" title="Link to this definition">¶</a></dt>
  209. <dd><p>Alias of <a class="reference internal" href="#zipfile.BadZipFile" title="zipfile.BadZipFile"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BadZipFile</span></code></a>, for compatibility with older Python versions.</p>
  210. <div class="deprecated">
  211. <p><span class="versionmodified deprecated">Deprecated since version 3.2.</span></p>
  212. </div>
  213. </dd></dl>
  214. <dl class="py exception">
  215. <dt class="sig sig-object py" id="zipfile.LargeZipFile">
  216. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">LargeZipFile</span></span><a class="headerlink" href="#zipfile.LargeZipFile" title="Link to this definition">¶</a></dt>
  217. <dd><p>The error raised when a ZIP file would require ZIP64 functionality but that has
  218. not been enabled.</p>
  219. </dd></dl>
  220. <dl class="py class">
  221. <dt class="sig sig-object py">
  222. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">ZipFile</span></span></dt>
  223. <dd><p>The class for reading and writing ZIP files. See section
  224. <a class="reference internal" href="#zipfile-objects"><span class="std std-ref">ZipFile Objects</span></a> for constructor details.</p>
  225. </dd></dl>
  226. <dl class="py class">
  227. <dt class="sig sig-object py">
  228. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">Path</span></span></dt>
  229. <dd><p>Class that implements a subset of the interface provided by
  230. <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">pathlib.Path</span></code></a>, including the full
  231. <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.Traversable" title="importlib.resources.abc.Traversable"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.Traversable</span></code></a> interface.</p>
  232. <div class="versionadded">
  233. <p><span class="versionmodified added">New in version 3.8.</span></p>
  234. </div>
  235. </dd></dl>
  236. <dl class="py class">
  237. <dt class="sig sig-object py">
  238. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">PyZipFile</span></span></dt>
  239. <dd><p>Class for creating ZIP archives containing Python libraries.</p>
  240. </dd></dl>
  241. <dl class="py class">
  242. <dt class="sig sig-object py" id="zipfile.ZipInfo">
  243. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">ZipInfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'NoName'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">date_time</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">(1980,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0)</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipInfo" title="Link to this definition">¶</a></dt>
  244. <dd><p>Class used to represent information about a member of an archive. Instances
  245. of this class are returned by the <a class="reference internal" href="#zipfile.ZipFile.getinfo" title="zipfile.ZipFile.getinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getinfo()</span></code></a> and <a class="reference internal" href="#zipfile.ZipFile.infolist" title="zipfile.ZipFile.infolist"><code class="xref py py-meth docutils literal notranslate"><span class="pre">infolist()</span></code></a>
  246. methods of <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> objects. Most users of the <a class="reference internal" href="#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> module
  247. will not need to create these, but only use those created by this
  248. module. <em>filename</em> should be the full name of the archive member, and
  249. <em>date_time</em> should be a tuple containing six fields which describe the time
  250. of the last modification to the file; the fields are described in section
  251. <a class="reference internal" href="#zipinfo-objects"><span class="std std-ref">ZipInfo Objects</span></a>.</p>
  252. </dd></dl>
  253. <dl class="py function">
  254. <dt class="sig sig-object py" id="zipfile.is_zipfile">
  255. <span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">is_zipfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.is_zipfile" title="Link to this definition">¶</a></dt>
  256. <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>filename</em> is a valid ZIP file based on its magic number,
  257. otherwise returns <code class="docutils literal notranslate"><span class="pre">False</span></code>. <em>filename</em> may be a file or file-like object too.</p>
  258. <div class="versionchanged">
  259. <p><span class="versionmodified changed">Changed in version 3.1: </span>Support for file and file-like objects.</p>
  260. </div>
  261. </dd></dl>
  262. <dl class="py data">
  263. <dt class="sig sig-object py" id="zipfile.ZIP_STORED">
  264. <span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">ZIP_STORED</span></span><a class="headerlink" href="#zipfile.ZIP_STORED" title="Link to this definition">¶</a></dt>
  265. <dd><p>The numeric constant for an uncompressed archive member.</p>
  266. </dd></dl>
  267. <dl class="py data">
  268. <dt class="sig sig-object py" id="zipfile.ZIP_DEFLATED">
  269. <span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">ZIP_DEFLATED</span></span><a class="headerlink" href="#zipfile.ZIP_DEFLATED" title="Link to this definition">¶</a></dt>
  270. <dd><p>The numeric constant for the usual ZIP compression method. This requires the
  271. <a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> module.</p>
  272. </dd></dl>
  273. <dl class="py data">
  274. <dt class="sig sig-object py" id="zipfile.ZIP_BZIP2">
  275. <span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">ZIP_BZIP2</span></span><a class="headerlink" href="#zipfile.ZIP_BZIP2" title="Link to this definition">¶</a></dt>
  276. <dd><p>The numeric constant for the BZIP2 compression method. This requires the
  277. <a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> module.</p>
  278. <div class="versionadded">
  279. <p><span class="versionmodified added">New in version 3.3.</span></p>
  280. </div>
  281. </dd></dl>
  282. <dl class="py data">
  283. <dt class="sig sig-object py" id="zipfile.ZIP_LZMA">
  284. <span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">ZIP_LZMA</span></span><a class="headerlink" href="#zipfile.ZIP_LZMA" title="Link to this definition">¶</a></dt>
  285. <dd><p>The numeric constant for the LZMA compression method. This requires the
  286. <a class="reference internal" href="lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> module.</p>
  287. <div class="versionadded">
  288. <p><span class="versionmodified added">New in version 3.3.</span></p>
  289. </div>
  290. <div class="admonition note">
  291. <p class="admonition-title">Note</p>
  292. <p>The ZIP file format specification has included support for bzip2 compression
  293. since 2001, and for LZMA compression since 2006. However, some tools
  294. (including older Python releases) do not support these compression
  295. methods, and may either refuse to process the ZIP file altogether,
  296. or fail to extract individual files.</p>
  297. </div>
  298. </dd></dl>
  299. <div class="admonition seealso">
  300. <p class="admonition-title">See also</p>
  301. <dl class="simple">
  302. <dt><a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a></dt><dd><p>Documentation on the ZIP file format by Phil Katz, the creator of the format and
  303. algorithms used.</p>
  304. </dd>
  305. <dt><a class="reference external" href="https://infozip.sourceforge.net/">Info-ZIP Home Page</a></dt><dd><p>Information about the Info-ZIP project’s ZIP archive programs and development
  306. libraries.</p>
  307. </dd>
  308. </dl>
  309. </div>
  310. <section id="zipfile-objects">
  311. <span id="id1"></span><h2>ZipFile Objects<a class="headerlink" href="#zipfile-objects" title="Link to this heading">¶</a></h2>
  312. <dl class="py class">
  313. <dt class="sig sig-object py" id="zipfile.ZipFile">
  314. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">ZipFile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compression</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">ZIP_STORED</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">allowZip64</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compresslevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strict_timestamps</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">metadata_encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile" title="Link to this definition">¶</a></dt>
  315. <dd><p>Open a ZIP file, where <em>file</em> can be a path to a file (a string), a
  316. file-like object or a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  317. <p>The <em>mode</em> parameter should be <code class="docutils literal notranslate"><span class="pre">'r'</span></code> to read an existing
  318. file, <code class="docutils literal notranslate"><span class="pre">'w'</span></code> to truncate and write a new file, <code class="docutils literal notranslate"><span class="pre">'a'</span></code> to append to an
  319. existing file, or <code class="docutils literal notranslate"><span class="pre">'x'</span></code> to exclusively create and write a new file.
  320. If <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">'x'</span></code> and <em>file</em> refers to an existing file,
  321. a <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> will be raised.
  322. If <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">'a'</span></code> and <em>file</em> refers to an existing ZIP
  323. file, then additional files are added to it. If <em>file</em> does not refer to a
  324. ZIP file, then a new ZIP archive is appended to the file. This is meant for
  325. adding a ZIP archive to another file (such as <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code>). If
  326. <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">'a'</span></code> and the file does not exist at all, it is created.
  327. If <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">'r'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a'</span></code>, the file should be seekable.</p>
  328. <p><em>compression</em> is the ZIP compression method to use when writing the archive,
  329. and should be <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_STORED</span></code></a>, <a class="reference internal" href="#zipfile.ZIP_DEFLATED" title="zipfile.ZIP_DEFLATED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_DEFLATED</span></code></a>,
  330. <a class="reference internal" href="#zipfile.ZIP_BZIP2" title="zipfile.ZIP_BZIP2"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_BZIP2</span></code></a> or <a class="reference internal" href="#zipfile.ZIP_LZMA" title="zipfile.ZIP_LZMA"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_LZMA</span></code></a>; unrecognized
  331. values will cause <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> to be raised. If
  332. <a class="reference internal" href="#zipfile.ZIP_DEFLATED" title="zipfile.ZIP_DEFLATED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_DEFLATED</span></code></a>, <a class="reference internal" href="#zipfile.ZIP_BZIP2" title="zipfile.ZIP_BZIP2"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_BZIP2</span></code></a> or <a class="reference internal" href="#zipfile.ZIP_LZMA" title="zipfile.ZIP_LZMA"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_LZMA</span></code></a> is specified
  333. but the corresponding module (<a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a>, <a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> or <a class="reference internal" href="lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a>) is not
  334. available, <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised. The default is <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_STORED</span></code></a>.</p>
  335. <p>If <em>allowZip64</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) zipfile will create ZIP files that
  336. use the ZIP64 extensions when the zipfile is larger than 4 GiB. If it is
  337. <code class="docutils literal notranslate"><span class="pre">false</span></code> <a class="reference internal" href="#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> will raise an exception when the ZIP file would
  338. require ZIP64 extensions.</p>
  339. <p>The <em>compresslevel</em> parameter controls the compression level to use when
  340. writing files to the archive.
  341. When using <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_STORED</span></code></a> or <a class="reference internal" href="#zipfile.ZIP_LZMA" title="zipfile.ZIP_LZMA"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_LZMA</span></code></a> it has no effect.
  342. When using <a class="reference internal" href="#zipfile.ZIP_DEFLATED" title="zipfile.ZIP_DEFLATED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_DEFLATED</span></code></a> integers <code class="docutils literal notranslate"><span class="pre">0</span></code> through <code class="docutils literal notranslate"><span class="pre">9</span></code> are accepted
  343. (see <a class="reference internal" href="zlib.html#zlib.compressobj" title="zlib.compressobj"><code class="xref py py-class docutils literal notranslate"><span class="pre">zlib</span></code></a> for more information).
  344. When using <a class="reference internal" href="#zipfile.ZIP_BZIP2" title="zipfile.ZIP_BZIP2"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_BZIP2</span></code></a> integers <code class="docutils literal notranslate"><span class="pre">1</span></code> through <code class="docutils literal notranslate"><span class="pre">9</span></code> are accepted
  345. (see <a class="reference internal" href="bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">bz2</span></code></a> for more information).</p>
  346. <p>The <em>strict_timestamps</em> argument, when set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, allows to
  347. zip files older than 1980-01-01 at the cost of setting the
  348. timestamp to 1980-01-01.
  349. Similar behavior occurs with files newer than 2107-12-31,
  350. the timestamp is also set to the limit.</p>
  351. <p>When mode is <code class="docutils literal notranslate"><span class="pre">'r'</span></code>, <em>metadata_encoding</em> may be set to the name of a codec,
  352. which will be used to decode metadata such as the names of members and ZIP
  353. comments.</p>
  354. <p>If the file is created with mode <code class="docutils literal notranslate"><span class="pre">'w'</span></code>, <code class="docutils literal notranslate"><span class="pre">'x'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a'</span></code> and then
  355. <a class="reference internal" href="#zipfile.ZipFile.close" title="zipfile.ZipFile.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">closed</span></code></a> without adding any files to the archive, the appropriate
  356. ZIP structures for an empty archive will be written to the file.</p>
  357. <p>ZipFile is also a context manager and therefore supports the
  358. <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement. In the example, <em>myzip</em> is closed after the
  359. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> statement’s suite is finished—even if an exception occurs:</p>
  360. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">ZipFile</span><span class="p">(</span><span class="s1">&#39;spam.zip&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">myzip</span><span class="p">:</span>
  361. <span class="n">myzip</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;eggs.txt&#39;</span><span class="p">)</span>
  362. </pre></div>
  363. </div>
  364. <div class="admonition note">
  365. <p class="admonition-title">Note</p>
  366. <p><em>metadata_encoding</em> is an instance-wide setting for the ZipFile.
  367. It is not currently possible to set this on a per-member basis.</p>
  368. <p>This attribute is a workaround for legacy implementations which produce
  369. archives with names in the current locale encoding or code page (mostly
  370. on Windows). According to the .ZIP standard, the encoding of metadata
  371. may be specified to be either IBM code page (default) or UTF-8 by a flag
  372. in the archive header.
  373. That flag takes precedence over <em>metadata_encoding</em>, which is
  374. a Python-specific extension.</p>
  375. </div>
  376. <div class="versionchanged">
  377. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the ability to use <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> as a context manager.</p>
  378. </div>
  379. <div class="versionchanged">
  380. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added support for <a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bzip2</span></code></a> and <a class="reference internal" href="lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> compression.</p>
  381. </div>
  382. <div class="versionchanged">
  383. <p><span class="versionmodified changed">Changed in version 3.4: </span>ZIP64 extensions are enabled by default.</p>
  384. </div>
  385. <div class="versionchanged">
  386. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for writing to unseekable streams.
  387. Added support for the <code class="docutils literal notranslate"><span class="pre">'x'</span></code> mode.</p>
  388. </div>
  389. <div class="versionchanged">
  390. <p><span class="versionmodified changed">Changed in version 3.6: </span>Previously, a plain <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised for unrecognized
  391. compression values.</p>
  392. </div>
  393. <div class="versionchanged">
  394. <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>file</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  395. </div>
  396. <div class="versionchanged">
  397. <p><span class="versionmodified changed">Changed in version 3.7: </span>Add the <em>compresslevel</em> parameter.</p>
  398. </div>
  399. <div class="versionchanged">
  400. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>strict_timestamps</em> keyword-only parameter.</p>
  401. </div>
  402. <div class="versionchanged">
  403. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added support for specifying member name encoding for reading
  404. metadata in the zipfile’s directory and file headers.</p>
  405. </div>
  406. </dd></dl>
  407. <dl class="py method">
  408. <dt class="sig sig-object py" id="zipfile.ZipFile.close">
  409. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.close" title="Link to this definition">¶</a></dt>
  410. <dd><p>Close the archive file. You must call <a class="reference internal" href="#zipfile.ZipFile.close" title="zipfile.ZipFile.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> before exiting your program
  411. or essential records will not be written.</p>
  412. </dd></dl>
  413. <dl class="py method">
  414. <dt class="sig sig-object py" id="zipfile.ZipFile.getinfo">
  415. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">getinfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.getinfo" title="Link to this definition">¶</a></dt>
  416. <dd><p>Return a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> object with information about the archive member
  417. <em>name</em>. Calling <a class="reference internal" href="#zipfile.ZipFile.getinfo" title="zipfile.ZipFile.getinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getinfo()</span></code></a> for a name not currently contained in the
  418. archive will raise a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>.</p>
  419. </dd></dl>
  420. <dl class="py method">
  421. <dt class="sig sig-object py" id="zipfile.ZipFile.infolist">
  422. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">infolist</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.infolist" title="Link to this definition">¶</a></dt>
  423. <dd><p>Return a list containing a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> object for each member of the
  424. archive. The objects are in the same order as their entries in the actual ZIP
  425. file on disk if an existing archive was opened.</p>
  426. </dd></dl>
  427. <dl class="py method">
  428. <dt class="sig sig-object py" id="zipfile.ZipFile.namelist">
  429. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">namelist</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.namelist" title="Link to this definition">¶</a></dt>
  430. <dd><p>Return a list of archive members by name.</p>
  431. </dd></dl>
  432. <dl class="py method">
  433. <dt class="sig sig-object py" id="zipfile.ZipFile.open">
  434. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pwd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">force_zip64</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.open" title="Link to this definition">¶</a></dt>
  435. <dd><p>Access a member of the archive as a binary file-like object. <em>name</em>
  436. can be either the name of a file within the archive or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a>
  437. object. The <em>mode</em> parameter, if included, must be <code class="docutils literal notranslate"><span class="pre">'r'</span></code> (the default)
  438. or <code class="docutils literal notranslate"><span class="pre">'w'</span></code>. <em>pwd</em> is the password used to decrypt encrypted ZIP files as a
  439. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object.</p>
  440. <p><a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code></a> is also a context manager and therefore supports the
  441. <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement:</p>
  442. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">ZipFile</span><span class="p">(</span><span class="s1">&#39;spam.zip&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">myzip</span><span class="p">:</span>
  443. <span class="k">with</span> <span class="n">myzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;eggs.txt&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">myfile</span><span class="p">:</span>
  444. <span class="nb">print</span><span class="p">(</span><span class="n">myfile</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
  445. </pre></div>
  446. </div>
  447. <p>With <em>mode</em> <code class="docutils literal notranslate"><span class="pre">'r'</span></code> the file-like object
  448. (<code class="docutils literal notranslate"><span class="pre">ZipExtFile</span></code>) is read-only and provides the following methods:
  449. <a class="reference internal" href="io.html#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a>, <a class="reference internal" href="io.html#io.IOBase.readline" title="io.IOBase.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a>,
  450. <a class="reference internal" href="io.html#io.IOBase.readlines" title="io.IOBase.readlines"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readlines()</span></code></a>, <a class="reference internal" href="io.html#io.IOBase.seek" title="io.IOBase.seek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code></a>,
  451. <a class="reference internal" href="io.html#io.IOBase.tell" title="io.IOBase.tell"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tell()</span></code></a>, <a class="reference internal" href="stdtypes.html#container.__iter__" title="container.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a>, <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a>.
  452. These objects can operate independently of the ZipFile.</p>
  453. <p>With <code class="docutils literal notranslate"><span class="pre">mode='w'</span></code>, a writable file handle is returned, which supports the
  454. <a class="reference internal" href="io.html#io.BufferedIOBase.write" title="io.BufferedIOBase.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> method. While a writable file handle is open,
  455. attempting to read or write other files in the ZIP file will raise a
  456. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  457. <p>When writing a file, if the file size is not known in advance but may exceed
  458. 2 GiB, pass <code class="docutils literal notranslate"><span class="pre">force_zip64=True</span></code> to ensure that the header format is
  459. capable of supporting large files. If the file size is known in advance,
  460. construct a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> object with <a class="reference internal" href="#zipfile.ZipInfo.file_size" title="zipfile.ZipInfo.file_size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">file_size</span></code></a> set, and
  461. use that as the <em>name</em> parameter.</p>
  462. <div class="admonition note">
  463. <p class="admonition-title">Note</p>
  464. <p>The <a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code></a>, <a class="reference internal" href="#zipfile.ZipFile.read" title="zipfile.ZipFile.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> and <a class="reference internal" href="#zipfile.ZipFile.extract" title="zipfile.ZipFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> methods can take a filename
  465. or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> object. You will appreciate this when trying to read a
  466. ZIP file that contains members with duplicate names.</p>
  467. </div>
  468. <div class="versionchanged">
  469. <p><span class="versionmodified changed">Changed in version 3.6: </span>Removed support of <code class="docutils literal notranslate"><span class="pre">mode='U'</span></code>. Use <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> for reading
  470. compressed text files in <a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a> mode.</p>
  471. </div>
  472. <div class="versionchanged">
  473. <p><span class="versionmodified changed">Changed in version 3.6: </span><a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ZipFile.open()</span></code></a> can now be used to write files into the archive with the
  474. <code class="docutils literal notranslate"><span class="pre">mode='w'</span></code> option.</p>
  475. </div>
  476. <div class="versionchanged">
  477. <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code></a> on a closed ZipFile will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.
  478. Previously, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised.</p>
  479. </div>
  480. </dd></dl>
  481. <dl class="py method">
  482. <dt class="sig sig-object py" id="zipfile.ZipFile.extract">
  483. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">extract</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">member</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pwd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.extract" title="Link to this definition">¶</a></dt>
  484. <dd><p>Extract a member from the archive to the current working directory; <em>member</em>
  485. must be its full name or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> object. Its file information is
  486. extracted as accurately as possible. <em>path</em> specifies a different directory
  487. to extract to. <em>member</em> can be a filename or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> object.
  488. <em>pwd</em> is the password used for encrypted files as a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object.</p>
  489. <p>Returns the normalized path created (a directory or new file).</p>
  490. <div class="admonition note">
  491. <p class="admonition-title">Note</p>
  492. <p>If a member filename is an absolute path, a drive/UNC sharepoint and
  493. leading (back)slashes will be stripped, e.g.: <code class="docutils literal notranslate"><span class="pre">///foo/bar</span></code> becomes
  494. <code class="docutils literal notranslate"><span class="pre">foo/bar</span></code> on Unix, and <code class="docutils literal notranslate"><span class="pre">C:\foo\bar</span></code> becomes <code class="docutils literal notranslate"><span class="pre">foo\bar</span></code> on Windows.
  495. And all <code class="docutils literal notranslate"><span class="pre">&quot;..&quot;</span></code> components in a member filename will be removed, e.g.:
  496. <code class="docutils literal notranslate"><span class="pre">../../foo../../ba..r</span></code> becomes <code class="docutils literal notranslate"><span class="pre">foo../ba..r</span></code>. On Windows illegal
  497. characters (<code class="docutils literal notranslate"><span class="pre">:</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">|</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">?</span></code>, and <code class="docutils literal notranslate"><span class="pre">*</span></code>)
  498. replaced by underscore (<code class="docutils literal notranslate"><span class="pre">_</span></code>).</p>
  499. </div>
  500. <div class="versionchanged">
  501. <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.extract" title="zipfile.ZipFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> on a closed ZipFile will raise a
  502. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>. Previously, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised.</p>
  503. </div>
  504. <div class="versionchanged">
  505. <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>path</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  506. </div>
  507. </dd></dl>
  508. <dl class="py method">
  509. <dt class="sig sig-object py" id="zipfile.ZipFile.extractall">
  510. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">extractall</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">members</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pwd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.extractall" title="Link to this definition">¶</a></dt>
  511. <dd><p>Extract all members from the archive to the current working directory. <em>path</em>
  512. specifies a different directory to extract to. <em>members</em> is optional and must
  513. be a subset of the list returned by <a class="reference internal" href="#zipfile.ZipFile.namelist" title="zipfile.ZipFile.namelist"><code class="xref py py-meth docutils literal notranslate"><span class="pre">namelist()</span></code></a>. <em>pwd</em> is the password
  514. used for encrypted files as a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object.</p>
  515. <div class="admonition warning">
  516. <p class="admonition-title">Warning</p>
  517. <p>Never extract archives from untrusted sources without prior inspection.
  518. It is possible that files are created outside of <em>path</em>, e.g. members
  519. that have absolute filenames starting with <code class="docutils literal notranslate"><span class="pre">&quot;/&quot;</span></code> or filenames with two
  520. dots <code class="docutils literal notranslate"><span class="pre">&quot;..&quot;</span></code>. This module attempts to prevent that.
  521. See <a class="reference internal" href="#zipfile.ZipFile.extract" title="zipfile.ZipFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> note.</p>
  522. </div>
  523. <div class="versionchanged">
  524. <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.extractall" title="zipfile.ZipFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a> on a closed ZipFile will raise a
  525. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>. Previously, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised.</p>
  526. </div>
  527. <div class="versionchanged">
  528. <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>path</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  529. </div>
  530. </dd></dl>
  531. <dl class="py method">
  532. <dt class="sig sig-object py" id="zipfile.ZipFile.printdir">
  533. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">printdir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.printdir" title="Link to this definition">¶</a></dt>
  534. <dd><p>Print a table of contents for the archive to <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>.</p>
  535. </dd></dl>
  536. <dl class="py method">
  537. <dt class="sig sig-object py" id="zipfile.ZipFile.setpassword">
  538. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">setpassword</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pwd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.setpassword" title="Link to this definition">¶</a></dt>
  539. <dd><p>Set <em>pwd</em> (a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object) as default password to extract encrypted files.</p>
  540. </dd></dl>
  541. <dl class="py method">
  542. <dt class="sig sig-object py" id="zipfile.ZipFile.read">
  543. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pwd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.read" title="Link to this definition">¶</a></dt>
  544. <dd><p>Return the bytes of the file <em>name</em> in the archive. <em>name</em> is the name of the
  545. file in the archive, or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> object. The archive must be open for
  546. read or append. <em>pwd</em> is the password used for encrypted files as a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>
  547. object and, if specified, overrides the default password set with <a class="reference internal" href="#zipfile.ZipFile.setpassword" title="zipfile.ZipFile.setpassword"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setpassword()</span></code></a>.
  548. Calling <a class="reference internal" href="#zipfile.ZipFile.read" title="zipfile.ZipFile.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> on a ZipFile that uses a compression method other than
  549. <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_STORED</span></code></a>, <a class="reference internal" href="#zipfile.ZIP_DEFLATED" title="zipfile.ZIP_DEFLATED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_DEFLATED</span></code></a>, <a class="reference internal" href="#zipfile.ZIP_BZIP2" title="zipfile.ZIP_BZIP2"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_BZIP2</span></code></a> or
  550. <a class="reference internal" href="#zipfile.ZIP_LZMA" title="zipfile.ZIP_LZMA"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_LZMA</span></code></a> will raise a <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>. An error will also
  551. be raised if the corresponding compression module is not available.</p>
  552. <div class="versionchanged">
  553. <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.read" title="zipfile.ZipFile.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> on a closed ZipFile will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.
  554. Previously, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised.</p>
  555. </div>
  556. </dd></dl>
  557. <dl class="py method">
  558. <dt class="sig sig-object py" id="zipfile.ZipFile.testzip">
  559. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">testzip</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.testzip" title="Link to this definition">¶</a></dt>
  560. <dd><p>Read all the files in the archive and check their CRC’s and file headers.
  561. Return the name of the first bad file, or else return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  562. <div class="versionchanged">
  563. <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.testzip" title="zipfile.ZipFile.testzip"><code class="xref py py-meth docutils literal notranslate"><span class="pre">testzip()</span></code></a> on a closed ZipFile will raise a
  564. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>. Previously, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised.</p>
  565. </div>
  566. </dd></dl>
  567. <dl class="py method">
  568. <dt class="sig sig-object py" id="zipfile.ZipFile.write">
  569. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arcname</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compress_type</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compresslevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.write" title="Link to this definition">¶</a></dt>
  570. <dd><p>Write the file named <em>filename</em> to the archive, giving it the archive name
  571. <em>arcname</em> (by default, this will be the same as <em>filename</em>, but without a drive
  572. letter and with leading path separators removed). If given, <em>compress_type</em>
  573. overrides the value given for the <em>compression</em> parameter to the constructor for
  574. the new entry. Similarly, <em>compresslevel</em> will override the constructor if
  575. given.
  576. The archive must be open with mode <code class="docutils literal notranslate"><span class="pre">'w'</span></code>, <code class="docutils literal notranslate"><span class="pre">'x'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a'</span></code>.</p>
  577. <div class="admonition note">
  578. <p class="admonition-title">Note</p>
  579. <p>The ZIP file standard historically did not specify a metadata encoding,
  580. but strongly recommended CP437 (the original IBM PC encoding) for
  581. interoperability. Recent versions allow use of UTF-8 (only). In this
  582. module, UTF-8 will automatically be used to write the member names if
  583. they contain any non-ASCII characters. It is not possible to write
  584. member names in any encoding other than ASCII or UTF-8.</p>
  585. </div>
  586. <div class="admonition note">
  587. <p class="admonition-title">Note</p>
  588. <p>Archive names should be relative to the archive root, that is, they should not
  589. start with a path separator.</p>
  590. </div>
  591. <div class="admonition note">
  592. <p class="admonition-title">Note</p>
  593. <p>If <code class="docutils literal notranslate"><span class="pre">arcname</span></code> (or <code class="docutils literal notranslate"><span class="pre">filename</span></code>, if <code class="docutils literal notranslate"><span class="pre">arcname</span></code> is not given) contains a null
  594. byte, the name of the file in the archive will be truncated at the null byte.</p>
  595. </div>
  596. <div class="admonition note">
  597. <p class="admonition-title">Note</p>
  598. <p>A leading slash in the filename may lead to the archive being impossible to
  599. open in some zip programs on Windows systems.</p>
  600. </div>
  601. <div class="versionchanged">
  602. <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.write" title="zipfile.ZipFile.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> on a ZipFile created with mode <code class="docutils literal notranslate"><span class="pre">'r'</span></code> or
  603. a closed ZipFile will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>. Previously,
  604. a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised.</p>
  605. </div>
  606. </dd></dl>
  607. <dl class="py method">
  608. <dt class="sig sig-object py" id="zipfile.ZipFile.writestr">
  609. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">writestr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">zinfo_or_arcname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compress_type</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compresslevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.writestr" title="Link to this definition">¶</a></dt>
  610. <dd><p>Write a file into the archive. The contents is <em>data</em>, which may be either
  611. a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> instance; if it is a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>,
  612. it is encoded as UTF-8 first. <em>zinfo_or_arcname</em> is either the file
  613. name it will be given in the archive, or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> instance. If it’s
  614. an instance, at least the filename, date, and time must be given. If it’s a
  615. name, the date and time is set to the current date and time.
  616. The archive must be opened with mode <code class="docutils literal notranslate"><span class="pre">'w'</span></code>, <code class="docutils literal notranslate"><span class="pre">'x'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a'</span></code>.</p>
  617. <p>If given, <em>compress_type</em> overrides the value given for the <em>compression</em>
  618. parameter to the constructor for the new entry, or in the <em>zinfo_or_arcname</em>
  619. (if that is a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> instance). Similarly, <em>compresslevel</em> will
  620. override the constructor if given.</p>
  621. <div class="admonition note">
  622. <p class="admonition-title">Note</p>
  623. <p>When passing a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> instance as the <em>zinfo_or_arcname</em> parameter,
  624. the compression method used will be that specified in the <em>compress_type</em>
  625. member of the given <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> instance. By default, the
  626. <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> constructor sets this member to <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_STORED</span></code></a>.</p>
  627. </div>
  628. <div class="versionchanged">
  629. <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>compress_type</em> argument.</p>
  630. </div>
  631. <div class="versionchanged">
  632. <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.writestr" title="zipfile.ZipFile.writestr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writestr()</span></code></a> on a ZipFile created with mode <code class="docutils literal notranslate"><span class="pre">'r'</span></code> or
  633. a closed ZipFile will raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>. Previously,
  634. a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> was raised.</p>
  635. </div>
  636. </dd></dl>
  637. <dl class="py method">
  638. <dt class="sig sig-object py" id="zipfile.ZipFile.mkdir">
  639. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">mkdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">zinfo_or_directory</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">511</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipFile.mkdir" title="Link to this definition">¶</a></dt>
  640. <dd><p>Create a directory inside the archive. If <em>zinfo_or_directory</em> is a string,
  641. a directory is created inside the archive with the mode that is specified in
  642. the <em>mode</em> argument. If, however, <em>zinfo_or_directory</em> is
  643. a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> instance then the <em>mode</em> argument is ignored.</p>
  644. <p>The archive must be opened with mode <code class="docutils literal notranslate"><span class="pre">'w'</span></code>, <code class="docutils literal notranslate"><span class="pre">'x'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a'</span></code>.</p>
  645. <div class="versionadded">
  646. <p><span class="versionmodified added">New in version 3.11.</span></p>
  647. </div>
  648. </dd></dl>
  649. <p>The following data attributes are also available:</p>
  650. <dl class="py attribute">
  651. <dt class="sig sig-object py" id="zipfile.ZipFile.filename">
  652. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#zipfile.ZipFile.filename" title="Link to this definition">¶</a></dt>
  653. <dd><p>Name of the ZIP file.</p>
  654. </dd></dl>
  655. <dl class="py attribute">
  656. <dt class="sig sig-object py" id="zipfile.ZipFile.debug">
  657. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">debug</span></span><a class="headerlink" href="#zipfile.ZipFile.debug" title="Link to this definition">¶</a></dt>
  658. <dd><p>The level of debug output to use. This may be set from <code class="docutils literal notranslate"><span class="pre">0</span></code> (the default, no
  659. output) to <code class="docutils literal notranslate"><span class="pre">3</span></code> (the most output). Debugging information is written to
  660. <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>.</p>
  661. </dd></dl>
  662. <dl class="py attribute">
  663. <dt class="sig sig-object py" id="zipfile.ZipFile.comment">
  664. <span class="sig-prename descclassname"><span class="pre">ZipFile.</span></span><span class="sig-name descname"><span class="pre">comment</span></span><a class="headerlink" href="#zipfile.ZipFile.comment" title="Link to this definition">¶</a></dt>
  665. <dd><p>The comment associated with the ZIP file as a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object.
  666. If assigning a comment to a
  667. <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> instance created with mode <code class="docutils literal notranslate"><span class="pre">'w'</span></code>, <code class="docutils literal notranslate"><span class="pre">'x'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a'</span></code>,
  668. it should be no longer than 65535 bytes. Comments longer than this will be
  669. truncated.</p>
  670. </dd></dl>
  671. </section>
  672. <section id="path-objects">
  673. <span id="id2"></span><h2>Path Objects<a class="headerlink" href="#path-objects" title="Link to this heading">¶</a></h2>
  674. <dl class="py class">
  675. <dt class="sig sig-object py" id="zipfile.Path">
  676. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">Path</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">root</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">at</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path" title="Link to this definition">¶</a></dt>
  677. <dd><p>Construct a Path object from a <code class="docutils literal notranslate"><span class="pre">root</span></code> zipfile (which may be a
  678. <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> instance or <code class="docutils literal notranslate"><span class="pre">file</span></code> suitable for passing to
  679. the <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> constructor).</p>
  680. <p><code class="docutils literal notranslate"><span class="pre">at</span></code> specifies the location of this Path within the zipfile,
  681. e.g. ‘dir/file.txt’, ‘dir/’, or ‘’. Defaults to the empty string,
  682. indicating the root.</p>
  683. </dd></dl>
  684. <p>Path objects expose the following features of <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib.Path</span></code></a>
  685. objects:</p>
  686. <p>Path objects are traversable using the <code class="docutils literal notranslate"><span class="pre">/</span></code> operator or <code class="docutils literal notranslate"><span class="pre">joinpath</span></code>.</p>
  687. <dl class="py attribute">
  688. <dt class="sig sig-object py" id="zipfile.Path.name">
  689. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#zipfile.Path.name" title="Link to this definition">¶</a></dt>
  690. <dd><p>The final path component.</p>
  691. </dd></dl>
  692. <dl class="py method">
  693. <dt class="sig sig-object py" id="zipfile.Path.open">
  694. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode='r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pwd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">**</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path.open" title="Link to this definition">¶</a></dt>
  695. <dd><p>Invoke <a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ZipFile.open()</span></code></a> on the current path.
  696. Allows opening for read or write, text or binary
  697. through supported modes: ‘r’, ‘w’, ‘rb’, ‘wb’.
  698. Positional and keyword arguments are passed through to
  699. <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> when opened as text and
  700. ignored otherwise.
  701. <code class="docutils literal notranslate"><span class="pre">pwd</span></code> is the <code class="docutils literal notranslate"><span class="pre">pwd</span></code> parameter to
  702. <a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ZipFile.open()</span></code></a>.</p>
  703. <div class="versionchanged">
  704. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added support for text and binary modes for open. Default
  705. mode is now text.</p>
  706. </div>
  707. <div class="versionchanged">
  708. <p><span class="versionmodified changed">Changed in version 3.11.2: </span>The <code class="docutils literal notranslate"><span class="pre">encoding</span></code> parameter can be supplied as a positional argument
  709. without causing a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. As it could in 3.9. Code needing to
  710. be compatible with unpatched 3.10 and 3.11 versions must pass all
  711. <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> arguments, <code class="docutils literal notranslate"><span class="pre">encoding</span></code> included, as keywords.</p>
  712. </div>
  713. </dd></dl>
  714. <dl class="py method">
  715. <dt class="sig sig-object py" id="zipfile.Path.iterdir">
  716. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">iterdir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path.iterdir" title="Link to this definition">¶</a></dt>
  717. <dd><p>Enumerate the children of the current directory.</p>
  718. </dd></dl>
  719. <dl class="py method">
  720. <dt class="sig sig-object py" id="zipfile.Path.is_dir">
  721. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_dir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path.is_dir" title="Link to this definition">¶</a></dt>
  722. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the current context references a directory.</p>
  723. </dd></dl>
  724. <dl class="py method">
  725. <dt class="sig sig-object py" id="zipfile.Path.is_file">
  726. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_file</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path.is_file" title="Link to this definition">¶</a></dt>
  727. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the current context references a file.</p>
  728. </dd></dl>
  729. <dl class="py method">
  730. <dt class="sig sig-object py" id="zipfile.Path.exists">
  731. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">exists</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path.exists" title="Link to this definition">¶</a></dt>
  732. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the current context references a file or
  733. directory in the zip file.</p>
  734. </dd></dl>
  735. <dl class="py data">
  736. <dt class="sig sig-object py" id="zipfile.Path.suffix">
  737. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">suffix</span></span><a class="headerlink" href="#zipfile.Path.suffix" title="Link to this definition">¶</a></dt>
  738. <dd><p>The file extension of the final component.</p>
  739. <div class="versionadded">
  740. <p><span class="versionmodified added">New in version 3.11: </span>Added <a class="reference internal" href="#zipfile.Path.suffix" title="zipfile.Path.suffix"><code class="xref py py-data docutils literal notranslate"><span class="pre">Path.suffix</span></code></a> property.</p>
  741. </div>
  742. </dd></dl>
  743. <dl class="py data">
  744. <dt class="sig sig-object py" id="zipfile.Path.stem">
  745. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">stem</span></span><a class="headerlink" href="#zipfile.Path.stem" title="Link to this definition">¶</a></dt>
  746. <dd><p>The final path component, without its suffix.</p>
  747. <div class="versionadded">
  748. <p><span class="versionmodified added">New in version 3.11: </span>Added <a class="reference internal" href="#zipfile.Path.stem" title="zipfile.Path.stem"><code class="xref py py-data docutils literal notranslate"><span class="pre">Path.stem</span></code></a> property.</p>
  749. </div>
  750. </dd></dl>
  751. <dl class="py data">
  752. <dt class="sig sig-object py" id="zipfile.Path.suffixes">
  753. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">suffixes</span></span><a class="headerlink" href="#zipfile.Path.suffixes" title="Link to this definition">¶</a></dt>
  754. <dd><p>A list of the path’s file extensions.</p>
  755. <div class="versionadded">
  756. <p><span class="versionmodified added">New in version 3.11: </span>Added <a class="reference internal" href="#zipfile.Path.suffixes" title="zipfile.Path.suffixes"><code class="xref py py-data docutils literal notranslate"><span class="pre">Path.suffixes</span></code></a> property.</p>
  757. </div>
  758. </dd></dl>
  759. <dl class="py method">
  760. <dt class="sig sig-object py" id="zipfile.Path.read_text">
  761. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">read_text</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">**</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path.read_text" title="Link to this definition">¶</a></dt>
  762. <dd><p>Read the current file as unicode text. Positional and
  763. keyword arguments are passed through to
  764. <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> (except <code class="docutils literal notranslate"><span class="pre">buffer</span></code>, which is
  765. implied by the context).</p>
  766. <div class="versionchanged">
  767. <p><span class="versionmodified changed">Changed in version 3.11.2: </span>The <code class="docutils literal notranslate"><span class="pre">encoding</span></code> parameter can be supplied as a positional argument
  768. without causing a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. As it could in 3.9. Code needing to
  769. be compatible with unpatched 3.10 and 3.11 versions must pass all
  770. <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> arguments, <code class="docutils literal notranslate"><span class="pre">encoding</span></code> included, as keywords.</p>
  771. </div>
  772. </dd></dl>
  773. <dl class="py method">
  774. <dt class="sig sig-object py" id="zipfile.Path.read_bytes">
  775. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">read_bytes</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path.read_bytes" title="Link to this definition">¶</a></dt>
  776. <dd><p>Read the current file as bytes.</p>
  777. </dd></dl>
  778. <dl class="py method">
  779. <dt class="sig sig-object py" id="zipfile.Path.joinpath">
  780. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">joinpath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path.joinpath" title="Link to this definition">¶</a></dt>
  781. <dd><p>Return a new Path object with each of the <em>other</em> arguments
  782. joined. The following are equivalent:</p>
  783. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Path</span><span class="p">(</span><span class="o">...</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="s1">&#39;child&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="s1">&#39;grandchild&#39;</span><span class="p">)</span>
  784. <span class="gp">&gt;&gt;&gt; </span><span class="n">Path</span><span class="p">(</span><span class="o">...</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="s1">&#39;child&#39;</span><span class="p">,</span> <span class="s1">&#39;grandchild&#39;</span><span class="p">)</span>
  785. <span class="gp">&gt;&gt;&gt; </span><span class="n">Path</span><span class="p">(</span><span class="o">...</span><span class="p">)</span> <span class="o">/</span> <span class="s1">&#39;child&#39;</span> <span class="o">/</span> <span class="s1">&#39;grandchild&#39;</span>
  786. </pre></div>
  787. </div>
  788. <div class="versionchanged">
  789. <p><span class="versionmodified changed">Changed in version 3.10: </span>Prior to 3.10, <code class="docutils literal notranslate"><span class="pre">joinpath</span></code> was undocumented and accepted
  790. exactly one parameter.</p>
  791. </div>
  792. </dd></dl>
  793. <p>The <a class="reference external" href="https://pypi.org/project/zipp">zipp</a> project provides backports
  794. of the latest path object functionality to older Pythons. Use
  795. <code class="docutils literal notranslate"><span class="pre">zipp.Path</span></code> in place of <code class="docutils literal notranslate"><span class="pre">zipfile.Path</span></code> for early access to
  796. changes.</p>
  797. </section>
  798. <section id="pyzipfile-objects">
  799. <span id="id3"></span><h2>PyZipFile Objects<a class="headerlink" href="#pyzipfile-objects" title="Link to this heading">¶</a></h2>
  800. <p>The <a class="reference internal" href="#zipfile.PyZipFile" title="zipfile.PyZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">PyZipFile</span></code></a> constructor takes the same parameters as the
  801. <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> constructor, and one additional parameter, <em>optimize</em>.</p>
  802. <dl class="py class">
  803. <dt class="sig sig-object py" id="zipfile.PyZipFile">
  804. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">PyZipFile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compression</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">ZIP_STORED</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">allowZip64</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">optimize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.PyZipFile" title="Link to this definition">¶</a></dt>
  805. <dd><div class="versionchanged">
  806. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>optimize</em> parameter.</p>
  807. </div>
  808. <div class="versionchanged">
  809. <p><span class="versionmodified changed">Changed in version 3.4: </span>ZIP64 extensions are enabled by default.</p>
  810. </div>
  811. <p>Instances have one method in addition to those of <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> objects:</p>
  812. <dl class="py method">
  813. <dt class="sig sig-object py" id="zipfile.PyZipFile.writepy">
  814. <span class="sig-name descname"><span class="pre">writepy</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pathname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">basename</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filterfunc</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.PyZipFile.writepy" title="Link to this definition">¶</a></dt>
  815. <dd><p>Search for files <code class="file docutils literal notranslate"><span class="pre">*.py</span></code> and add the corresponding file to the
  816. archive.</p>
  817. <p>If the <em>optimize</em> parameter to <a class="reference internal" href="#zipfile.PyZipFile" title="zipfile.PyZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">PyZipFile</span></code></a> was not given or <code class="docutils literal notranslate"><span class="pre">-1</span></code>,
  818. the corresponding file is a <code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code> file, compiling if necessary.</p>
  819. <p>If the <em>optimize</em> parameter to <a class="reference internal" href="#zipfile.PyZipFile" title="zipfile.PyZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">PyZipFile</span></code></a> was <code class="docutils literal notranslate"><span class="pre">0</span></code>, <code class="docutils literal notranslate"><span class="pre">1</span></code> or
  820. <code class="docutils literal notranslate"><span class="pre">2</span></code>, only files with that optimization level (see <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>) are
  821. added to the archive, compiling if necessary.</p>
  822. <p>If <em>pathname</em> is a file, the filename must end with <code class="file docutils literal notranslate"><span class="pre">.py</span></code>, and
  823. just the (corresponding <code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code>) file is added at the top level
  824. (no path information). If <em>pathname</em> is a file that does not end with
  825. <code class="file docutils literal notranslate"><span class="pre">.py</span></code>, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> will be raised. If it is a directory,
  826. and the directory is not a package directory, then all the files
  827. <code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code> are added at the top level. If the directory is a
  828. package directory, then all <code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code> are added under the package
  829. name as a file path, and if any subdirectories are package directories,
  830. all of these are added recursively in sorted order.</p>
  831. <p><em>basename</em> is intended for internal use only.</p>
  832. <p><em>filterfunc</em>, if given, must be a function taking a single string
  833. argument. It will be passed each path (including each individual full
  834. file path) before it is added to the archive. If <em>filterfunc</em> returns a
  835. false value, the path will not be added, and if it is a directory its
  836. contents will be ignored. For example, if our test files are all either
  837. in <code class="docutils literal notranslate"><span class="pre">test</span></code> directories or start with the string <code class="docutils literal notranslate"><span class="pre">test_</span></code>, we can use a
  838. <em>filterfunc</em> to exclude them:</p>
  839. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">zf</span> <span class="o">=</span> <span class="n">PyZipFile</span><span class="p">(</span><span class="s1">&#39;myprog.zip&#39;</span><span class="p">)</span>
  840. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">notests</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
  841. <span class="gp">... </span> <span class="n">fn</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  842. <span class="gp">... </span> <span class="k">return</span> <span class="p">(</span><span class="ow">not</span> <span class="p">(</span><span class="n">fn</span> <span class="o">==</span> <span class="s1">&#39;test&#39;</span> <span class="ow">or</span> <span class="n">fn</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;test_&#39;</span><span class="p">)))</span>
  843. <span class="gp">...</span>
  844. <span class="gp">&gt;&gt;&gt; </span><span class="n">zf</span><span class="o">.</span><span class="n">writepy</span><span class="p">(</span><span class="s1">&#39;myprog&#39;</span><span class="p">,</span> <span class="n">filterfunc</span><span class="o">=</span><span class="n">notests</span><span class="p">)</span>
  845. </pre></div>
  846. </div>
  847. <p>The <a class="reference internal" href="#zipfile.PyZipFile.writepy" title="zipfile.PyZipFile.writepy"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writepy()</span></code></a> method makes archives with file names like
  848. this:</p>
  849. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">string</span><span class="o">.</span><span class="n">pyc</span> <span class="c1"># Top level name</span>
  850. <span class="n">test</span><span class="o">/</span><span class="fm">__init__</span><span class="o">.</span><span class="n">pyc</span> <span class="c1"># Package directory</span>
  851. <span class="n">test</span><span class="o">/</span><span class="n">testall</span><span class="o">.</span><span class="n">pyc</span> <span class="c1"># Module test.testall</span>
  852. <span class="n">test</span><span class="o">/</span><span class="n">bogus</span><span class="o">/</span><span class="fm">__init__</span><span class="o">.</span><span class="n">pyc</span> <span class="c1"># Subpackage directory</span>
  853. <span class="n">test</span><span class="o">/</span><span class="n">bogus</span><span class="o">/</span><span class="n">myfile</span><span class="o">.</span><span class="n">pyc</span> <span class="c1"># Submodule test.bogus.myfile</span>
  854. </pre></div>
  855. </div>
  856. <div class="versionchanged">
  857. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>filterfunc</em> parameter.</p>
  858. </div>
  859. <div class="versionchanged">
  860. <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>pathname</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  861. </div>
  862. <div class="versionchanged">
  863. <p><span class="versionmodified changed">Changed in version 3.7: </span>Recursion sorts directory entries.</p>
  864. </div>
  865. </dd></dl>
  866. </dd></dl>
  867. </section>
  868. <section id="zipinfo-objects">
  869. <span id="id4"></span><h2>ZipInfo Objects<a class="headerlink" href="#zipinfo-objects" title="Link to this heading">¶</a></h2>
  870. <p>Instances of the <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> class are returned by the <a class="reference internal" href="#zipfile.ZipFile.getinfo" title="zipfile.ZipFile.getinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getinfo()</span></code></a> and
  871. <a class="reference internal" href="#zipfile.ZipFile.infolist" title="zipfile.ZipFile.infolist"><code class="xref py py-meth docutils literal notranslate"><span class="pre">infolist()</span></code></a> methods of <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> objects. Each object stores
  872. information about a single member of the ZIP archive.</p>
  873. <p>There is one classmethod to make a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> instance for a filesystem
  874. file:</p>
  875. <dl class="py method">
  876. <dt class="sig sig-object py" id="zipfile.ZipInfo.from_file">
  877. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">from_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">arcname</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strict_timestamps</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipInfo.from_file" title="Link to this definition">¶</a></dt>
  878. <dd><p>Construct a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code></a> instance for a file on the filesystem, in
  879. preparation for adding it to a zip file.</p>
  880. <p><em>filename</em> should be the path to a file or directory on the filesystem.</p>
  881. <p>If <em>arcname</em> is specified, it is used as the name within the archive.
  882. If <em>arcname</em> is not specified, the name will be the same as <em>filename</em>, but
  883. with any drive letter and leading path separators removed.</p>
  884. <p>The <em>strict_timestamps</em> argument, when set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, allows to
  885. zip files older than 1980-01-01 at the cost of setting the
  886. timestamp to 1980-01-01.
  887. Similar behavior occurs with files newer than 2107-12-31,
  888. the timestamp is also set to the limit.</p>
  889. <div class="versionadded">
  890. <p><span class="versionmodified added">New in version 3.6.</span></p>
  891. </div>
  892. <div class="versionchanged">
  893. <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>filename</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  894. </div>
  895. <div class="versionchanged">
  896. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>strict_timestamps</em> keyword-only parameter.</p>
  897. </div>
  898. </dd></dl>
  899. <p>Instances have the following methods and attributes:</p>
  900. <dl class="py method">
  901. <dt class="sig sig-object py" id="zipfile.ZipInfo.is_dir">
  902. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">is_dir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipInfo.is_dir" title="Link to this definition">¶</a></dt>
  903. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if this archive member is a directory.</p>
  904. <p>This uses the entry’s name: directories should always end with <code class="docutils literal notranslate"><span class="pre">/</span></code>.</p>
  905. <div class="versionadded">
  906. <p><span class="versionmodified added">New in version 3.6.</span></p>
  907. </div>
  908. </dd></dl>
  909. <dl class="py attribute">
  910. <dt class="sig sig-object py" id="zipfile.ZipInfo.filename">
  911. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#zipfile.ZipInfo.filename" title="Link to this definition">¶</a></dt>
  912. <dd><p>Name of the file in the archive.</p>
  913. </dd></dl>
  914. <dl class="py attribute">
  915. <dt class="sig sig-object py" id="zipfile.ZipInfo.date_time">
  916. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">date_time</span></span><a class="headerlink" href="#zipfile.ZipInfo.date_time" title="Link to this definition">¶</a></dt>
  917. <dd><p>The time and date of the last modification to the archive member. This is a
  918. tuple of six values:</p>
  919. <table class="docutils align-default">
  920. <thead>
  921. <tr class="row-odd"><th class="head"><p>Index</p></th>
  922. <th class="head"><p>Value</p></th>
  923. </tr>
  924. </thead>
  925. <tbody>
  926. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">0</span></code></p></td>
  927. <td><p>Year (&gt;= 1980)</p></td>
  928. </tr>
  929. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">1</span></code></p></td>
  930. <td><p>Month (one-based)</p></td>
  931. </tr>
  932. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">2</span></code></p></td>
  933. <td><p>Day of month (one-based)</p></td>
  934. </tr>
  935. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">3</span></code></p></td>
  936. <td><p>Hours (zero-based)</p></td>
  937. </tr>
  938. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">4</span></code></p></td>
  939. <td><p>Minutes (zero-based)</p></td>
  940. </tr>
  941. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">5</span></code></p></td>
  942. <td><p>Seconds (zero-based)</p></td>
  943. </tr>
  944. </tbody>
  945. </table>
  946. <div class="admonition note">
  947. <p class="admonition-title">Note</p>
  948. <p>The ZIP file format does not support timestamps before 1980.</p>
  949. </div>
  950. </dd></dl>
  951. <dl class="py attribute">
  952. <dt class="sig sig-object py" id="zipfile.ZipInfo.compress_type">
  953. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">compress_type</span></span><a class="headerlink" href="#zipfile.ZipInfo.compress_type" title="Link to this definition">¶</a></dt>
  954. <dd><p>Type of compression for the archive member.</p>
  955. </dd></dl>
  956. <dl class="py attribute">
  957. <dt class="sig sig-object py" id="zipfile.ZipInfo.comment">
  958. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">comment</span></span><a class="headerlink" href="#zipfile.ZipInfo.comment" title="Link to this definition">¶</a></dt>
  959. <dd><p>Comment for the individual archive member as a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object.</p>
  960. </dd></dl>
  961. <dl class="py attribute">
  962. <dt class="sig sig-object py" id="zipfile.ZipInfo.extra">
  963. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">extra</span></span><a class="headerlink" href="#zipfile.ZipInfo.extra" title="Link to this definition">¶</a></dt>
  964. <dd><p>Expansion field data. The <a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a> contains
  965. some comments on the internal structure of the data contained in this
  966. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object.</p>
  967. </dd></dl>
  968. <dl class="py attribute">
  969. <dt class="sig sig-object py" id="zipfile.ZipInfo.create_system">
  970. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">create_system</span></span><a class="headerlink" href="#zipfile.ZipInfo.create_system" title="Link to this definition">¶</a></dt>
  971. <dd><p>System which created ZIP archive.</p>
  972. </dd></dl>
  973. <dl class="py attribute">
  974. <dt class="sig sig-object py" id="zipfile.ZipInfo.create_version">
  975. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">create_version</span></span><a class="headerlink" href="#zipfile.ZipInfo.create_version" title="Link to this definition">¶</a></dt>
  976. <dd><p>PKZIP version which created ZIP archive.</p>
  977. </dd></dl>
  978. <dl class="py attribute">
  979. <dt class="sig sig-object py" id="zipfile.ZipInfo.extract_version">
  980. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">extract_version</span></span><a class="headerlink" href="#zipfile.ZipInfo.extract_version" title="Link to this definition">¶</a></dt>
  981. <dd><p>PKZIP version needed to extract archive.</p>
  982. </dd></dl>
  983. <dl class="py attribute">
  984. <dt class="sig sig-object py" id="zipfile.ZipInfo.reserved">
  985. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">reserved</span></span><a class="headerlink" href="#zipfile.ZipInfo.reserved" title="Link to this definition">¶</a></dt>
  986. <dd><p>Must be zero.</p>
  987. </dd></dl>
  988. <dl class="py attribute">
  989. <dt class="sig sig-object py" id="zipfile.ZipInfo.flag_bits">
  990. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">flag_bits</span></span><a class="headerlink" href="#zipfile.ZipInfo.flag_bits" title="Link to this definition">¶</a></dt>
  991. <dd><p>ZIP flag bits.</p>
  992. </dd></dl>
  993. <dl class="py attribute">
  994. <dt class="sig sig-object py" id="zipfile.ZipInfo.volume">
  995. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">volume</span></span><a class="headerlink" href="#zipfile.ZipInfo.volume" title="Link to this definition">¶</a></dt>
  996. <dd><p>Volume number of file header.</p>
  997. </dd></dl>
  998. <dl class="py attribute">
  999. <dt class="sig sig-object py" id="zipfile.ZipInfo.internal_attr">
  1000. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">internal_attr</span></span><a class="headerlink" href="#zipfile.ZipInfo.internal_attr" title="Link to this definition">¶</a></dt>
  1001. <dd><p>Internal attributes.</p>
  1002. </dd></dl>
  1003. <dl class="py attribute">
  1004. <dt class="sig sig-object py" id="zipfile.ZipInfo.external_attr">
  1005. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">external_attr</span></span><a class="headerlink" href="#zipfile.ZipInfo.external_attr" title="Link to this definition">¶</a></dt>
  1006. <dd><p>External file attributes.</p>
  1007. </dd></dl>
  1008. <dl class="py attribute">
  1009. <dt class="sig sig-object py" id="zipfile.ZipInfo.header_offset">
  1010. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">header_offset</span></span><a class="headerlink" href="#zipfile.ZipInfo.header_offset" title="Link to this definition">¶</a></dt>
  1011. <dd><p>Byte offset to the file header.</p>
  1012. </dd></dl>
  1013. <dl class="py attribute">
  1014. <dt class="sig sig-object py" id="zipfile.ZipInfo.CRC">
  1015. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">CRC</span></span><a class="headerlink" href="#zipfile.ZipInfo.CRC" title="Link to this definition">¶</a></dt>
  1016. <dd><p>CRC-32 of the uncompressed file.</p>
  1017. </dd></dl>
  1018. <dl class="py attribute">
  1019. <dt class="sig sig-object py" id="zipfile.ZipInfo.compress_size">
  1020. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">compress_size</span></span><a class="headerlink" href="#zipfile.ZipInfo.compress_size" title="Link to this definition">¶</a></dt>
  1021. <dd><p>Size of the compressed data.</p>
  1022. </dd></dl>
  1023. <dl class="py attribute">
  1024. <dt class="sig sig-object py" id="zipfile.ZipInfo.file_size">
  1025. <span class="sig-prename descclassname"><span class="pre">ZipInfo.</span></span><span class="sig-name descname"><span class="pre">file_size</span></span><a class="headerlink" href="#zipfile.ZipInfo.file_size" title="Link to this definition">¶</a></dt>
  1026. <dd><p>Size of the uncompressed file.</p>
  1027. </dd></dl>
  1028. </section>
  1029. <section id="command-line-interface">
  1030. <span id="zipfile-commandline"></span><h2>Command-Line Interface<a class="headerlink" href="#command-line-interface" title="Link to this heading">¶</a></h2>
  1031. <p>The <a class="reference internal" href="#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> module provides a simple command-line interface to interact
  1032. with ZIP archives.</p>
  1033. <p>If you want to create a new ZIP archive, specify its name after the <a class="reference internal" href="#cmdoption-zipfile-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a>
  1034. option and then list the filename(s) that should be included:</p>
  1035. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipfile<span class="w"> </span>-c<span class="w"> </span>monty.zip<span class="w"> </span>spam.txt<span class="w"> </span>eggs.txt
  1036. </pre></div>
  1037. </div>
  1038. <p>Passing a directory is also acceptable:</p>
  1039. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipfile<span class="w"> </span>-c<span class="w"> </span>monty.zip<span class="w"> </span>life-of-brian_1979/
  1040. </pre></div>
  1041. </div>
  1042. <p>If you want to extract a ZIP archive into the specified directory, use
  1043. the <a class="reference internal" href="#cmdoption-zipfile-e"><code class="xref std std-option docutils literal notranslate"><span class="pre">-e</span></code></a> option:</p>
  1044. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipfile<span class="w"> </span>-e<span class="w"> </span>monty.zip<span class="w"> </span>target-dir/
  1045. </pre></div>
  1046. </div>
  1047. <p>For a list of the files in a ZIP archive, use the <a class="reference internal" href="#cmdoption-zipfile-l"><code class="xref std std-option docutils literal notranslate"><span class="pre">-l</span></code></a> option:</p>
  1048. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipfile<span class="w"> </span>-l<span class="w"> </span>monty.zip
  1049. </pre></div>
  1050. </div>
  1051. <section id="command-line-options">
  1052. <h3>Command-line options<a class="headerlink" href="#command-line-options" title="Link to this heading">¶</a></h3>
  1053. <dl class="std option">
  1054. <dt class="sig sig-object std" id="cmdoption-zipfile-l">
  1055. <span class="sig-name descname"><span class="pre">-l</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;zipfile&gt;</span></span><a class="headerlink" href="#cmdoption-zipfile-l" title="Link to this definition">¶</a></dt>
  1056. <dt class="sig sig-object std" id="cmdoption-zipfile-list">
  1057. <span class="sig-name descname"><span class="pre">--list</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;zipfile&gt;</span></span><a class="headerlink" href="#cmdoption-zipfile-list" title="Link to this definition">¶</a></dt>
  1058. <dd><p>List files in a zipfile.</p>
  1059. </dd></dl>
  1060. <dl class="std option">
  1061. <dt class="sig sig-object std" id="cmdoption-zipfile-c">
  1062. <span class="sig-name descname"><span class="pre">-c</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;zipfile&gt;</span> <span class="pre">&lt;source1&gt;</span> <span class="pre">...</span> <span class="pre">&lt;sourceN&gt;</span></span><a class="headerlink" href="#cmdoption-zipfile-c" title="Link to this definition">¶</a></dt>
  1063. <dt class="sig sig-object std" id="cmdoption-zipfile-create">
  1064. <span class="sig-name descname"><span class="pre">--create</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;zipfile&gt;</span> <span class="pre">&lt;source1&gt;</span> <span class="pre">...</span> <span class="pre">&lt;sourceN&gt;</span></span><a class="headerlink" href="#cmdoption-zipfile-create" title="Link to this definition">¶</a></dt>
  1065. <dd><p>Create zipfile from source files.</p>
  1066. </dd></dl>
  1067. <dl class="std option">
  1068. <dt class="sig sig-object std" id="cmdoption-zipfile-e">
  1069. <span class="sig-name descname"><span class="pre">-e</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;zipfile&gt;</span> <span class="pre">&lt;output_dir&gt;</span></span><a class="headerlink" href="#cmdoption-zipfile-e" title="Link to this definition">¶</a></dt>
  1070. <dt class="sig sig-object std" id="cmdoption-zipfile-extract">
  1071. <span class="sig-name descname"><span class="pre">--extract</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;zipfile&gt;</span> <span class="pre">&lt;output_dir&gt;</span></span><a class="headerlink" href="#cmdoption-zipfile-extract" title="Link to this definition">¶</a></dt>
  1072. <dd><p>Extract zipfile into target directory.</p>
  1073. </dd></dl>
  1074. <dl class="std option">
  1075. <dt class="sig sig-object std" id="cmdoption-zipfile-t">
  1076. <span class="sig-name descname"><span class="pre">-t</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;zipfile&gt;</span></span><a class="headerlink" href="#cmdoption-zipfile-t" title="Link to this definition">¶</a></dt>
  1077. <dt class="sig sig-object std" id="cmdoption-zipfile-test">
  1078. <span class="sig-name descname"><span class="pre">--test</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;zipfile&gt;</span></span><a class="headerlink" href="#cmdoption-zipfile-test" title="Link to this definition">¶</a></dt>
  1079. <dd><p>Test whether the zipfile is valid or not.</p>
  1080. </dd></dl>
  1081. <dl class="std option">
  1082. <dt class="sig sig-object std" id="cmdoption-zipfile-metadata-encoding">
  1083. <span class="sig-name descname"><span class="pre">--metadata-encoding</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;encoding&gt;</span></span><a class="headerlink" href="#cmdoption-zipfile-metadata-encoding" title="Link to this definition">¶</a></dt>
  1084. <dd><p>Specify encoding of member names for <a class="reference internal" href="#cmdoption-zipfile-l"><code class="xref std std-option docutils literal notranslate"><span class="pre">-l</span></code></a>, <a class="reference internal" href="#cmdoption-zipfile-e"><code class="xref std std-option docutils literal notranslate"><span class="pre">-e</span></code></a> and
  1085. <a class="reference internal" href="#cmdoption-zipfile-t"><code class="xref std std-option docutils literal notranslate"><span class="pre">-t</span></code></a>.</p>
  1086. <div class="versionadded">
  1087. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1088. </div>
  1089. </dd></dl>
  1090. </section>
  1091. </section>
  1092. <section id="decompression-pitfalls">
  1093. <h2>Decompression pitfalls<a class="headerlink" href="#decompression-pitfalls" title="Link to this heading">¶</a></h2>
  1094. <p>The extraction in zipfile module might fail due to some pitfalls listed below.</p>
  1095. <section id="from-file-itself">
  1096. <h3>From file itself<a class="headerlink" href="#from-file-itself" title="Link to this heading">¶</a></h3>
  1097. <p>Decompression may fail due to incorrect password / CRC checksum / ZIP format or
  1098. unsupported compression method / decryption.</p>
  1099. </section>
  1100. <section id="file-system-limitations">
  1101. <h3>File System limitations<a class="headerlink" href="#file-system-limitations" title="Link to this heading">¶</a></h3>
  1102. <p>Exceeding limitations on different file systems can cause decompression failed.
  1103. Such as allowable characters in the directory entries, length of the file name,
  1104. length of the pathname, size of a single file, and number of files, etc.</p>
  1105. </section>
  1106. <section id="resources-limitations">
  1107. <span id="zipfile-resources-limitations"></span><h3>Resources limitations<a class="headerlink" href="#resources-limitations" title="Link to this heading">¶</a></h3>
  1108. <p>The lack of memory or disk volume would lead to decompression
  1109. failed. For example, decompression bombs (aka <a class="reference external" href="https://en.wikipedia.org/wiki/Zip_bomb">ZIP bomb</a>)
  1110. apply to zipfile library that can cause disk volume exhaustion.</p>
  1111. </section>
  1112. <section id="interruption">
  1113. <h3>Interruption<a class="headerlink" href="#interruption" title="Link to this heading">¶</a></h3>
  1114. <p>Interruption during the decompression, such as pressing control-C or killing the
  1115. decompression process may result in incomplete decompression of the archive.</p>
  1116. </section>
  1117. <section id="default-behaviors-of-extraction">
  1118. <h3>Default behaviors of extraction<a class="headerlink" href="#default-behaviors-of-extraction" title="Link to this heading">¶</a></h3>
  1119. <p>Not knowing the default extraction behaviors
  1120. can cause unexpected decompression results.
  1121. For example, when extracting the same archive twice,
  1122. it overwrites files without asking.</p>
  1123. </section>
  1124. </section>
  1125. </section>
  1126. <div class="clearer"></div>
  1127. </div>
  1128. </div>
  1129. </div>
  1130. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1131. <div class="sphinxsidebarwrapper">
  1132. <div>
  1133. <h3><a href="../contents.html">Table of Contents</a></h3>
  1134. <ul>
  1135. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> — Work with ZIP archives</a><ul>
  1136. <li><a class="reference internal" href="#zipfile-objects">ZipFile Objects</a></li>
  1137. <li><a class="reference internal" href="#path-objects">Path Objects</a></li>
  1138. <li><a class="reference internal" href="#pyzipfile-objects">PyZipFile Objects</a></li>
  1139. <li><a class="reference internal" href="#zipinfo-objects">ZipInfo Objects</a></li>
  1140. <li><a class="reference internal" href="#command-line-interface">Command-Line Interface</a><ul>
  1141. <li><a class="reference internal" href="#command-line-options">Command-line options</a></li>
  1142. </ul>
  1143. </li>
  1144. <li><a class="reference internal" href="#decompression-pitfalls">Decompression pitfalls</a><ul>
  1145. <li><a class="reference internal" href="#from-file-itself">From file itself</a></li>
  1146. <li><a class="reference internal" href="#file-system-limitations">File System limitations</a></li>
  1147. <li><a class="reference internal" href="#resources-limitations">Resources limitations</a></li>
  1148. <li><a class="reference internal" href="#interruption">Interruption</a></li>
  1149. <li><a class="reference internal" href="#default-behaviors-of-extraction">Default behaviors of extraction</a></li>
  1150. </ul>
  1151. </li>
  1152. </ul>
  1153. </li>
  1154. </ul>
  1155. </div>
  1156. <div>
  1157. <h4>Previous topic</h4>
  1158. <p class="topless"><a href="lzma.html"
  1159. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code> — Compression using the LZMA algorithm</a></p>
  1160. </div>
  1161. <div>
  1162. <h4>Next topic</h4>
  1163. <p class="topless"><a href="tarfile.html"
  1164. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code> — Read and write tar archive files</a></p>
  1165. </div>
  1166. <div role="note" aria-label="source link">
  1167. <h3>This Page</h3>
  1168. <ul class="this-page-menu">
  1169. <li><a href="../bugs.html">Report a Bug</a></li>
  1170. <li>
  1171. <a href="https://github.com/python/cpython/blob/main/Doc/library/zipfile.rst"
  1172. rel="nofollow">Show Source
  1173. </a>
  1174. </li>
  1175. </ul>
  1176. </div>
  1177. </div>
  1178. <div id="sidebarbutton" title="Collapse sidebar">
  1179. <span>«</span>
  1180. </div>
  1181. </div>
  1182. <div class="clearer"></div>
  1183. </div>
  1184. <div class="related" role="navigation" aria-label="related navigation">
  1185. <h3>Navigation</h3>
  1186. <ul>
  1187. <li class="right" style="margin-right: 10px">
  1188. <a href="../genindex.html" title="General Index"
  1189. >index</a></li>
  1190. <li class="right" >
  1191. <a href="../py-modindex.html" title="Python Module Index"
  1192. >modules</a> |</li>
  1193. <li class="right" >
  1194. <a href="tarfile.html" title="tarfile — Read and write tar archive files"
  1195. >next</a> |</li>
  1196. <li class="right" >
  1197. <a href="lzma.html" title="lzma — Compression using the LZMA algorithm"
  1198. >previous</a> |</li>
  1199. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1200. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1201. <li class="switchers">
  1202. <div class="language_switcher_placeholder"></div>
  1203. <div class="version_switcher_placeholder"></div>
  1204. </li>
  1205. <li>
  1206. </li>
  1207. <li id="cpython-language-and-version">
  1208. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1209. </li>
  1210. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1211. <li class="nav-item nav-item-2"><a href="archiving.html" >Data Compression and Archiving</a> &#187;</li>
  1212. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> — Work with ZIP archives</a></li>
  1213. <li class="right">
  1214. <div class="inline-search" role="search">
  1215. <form class="inline-search" action="../search.html" method="get">
  1216. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1217. <input type="submit" value="Go" />
  1218. </form>
  1219. </div>
  1220. |
  1221. </li>
  1222. <li class="right">
  1223. <label class="theme-selector-label">
  1224. Theme
  1225. <select class="theme-selector" oninput="activateTheme(this.value)">
  1226. <option value="auto" selected>Auto</option>
  1227. <option value="light">Light</option>
  1228. <option value="dark">Dark</option>
  1229. </select>
  1230. </label> |</li>
  1231. </ul>
  1232. </div>
  1233. <div class="footer">
  1234. &copy;
  1235. <a href="../copyright.html">
  1236. Copyright
  1237. </a>
  1238. 2001-2024, Python Software Foundation.
  1239. <br />
  1240. This page is licensed under the Python Software Foundation License Version 2.
  1241. <br />
  1242. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1243. <br />
  1244. See <a href="/license.html">History and License</a> for more information.<br />
  1245. <br />
  1246. The Python Software Foundation is a non-profit corporation.
  1247. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1248. <br />
  1249. <br />
  1250. Last updated on Apr 09, 2024 (13:47 UTC).
  1251. <a href="/bugs.html">Found a bug</a>?
  1252. <br />
  1253. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1254. </div>
  1255. </body>
  1256. </html>
上海开阖软件有限公司 沪ICP备12045867号-1