parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="zipfile — Work with ZIP archives" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/zipfile.html" />10<meta property="og:site_name" content="Python documentation" />11<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 ..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_zipfile_14713b9a.png" />15<meta property="og:image:alt" 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 ..." />16<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 ..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>zipfile — Work with ZIP archives — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="tarfile — Read and write tar archive files" href="tarfile.html" />43 <link rel="prev" title="lzma — Compression using the LZMA algorithm" href="lzma.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/zipfile.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <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>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<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>108<li><a class="reference internal" href="#zipfile-objects">ZipFile objects</a></li>109<li><a class="reference internal" href="#path-objects">Path objects</a></li>110<li><a class="reference internal" href="#pyzipfile-objects">PyZipFile objects</a></li>111<li><a class="reference internal" href="#zipinfo-objects">ZipInfo objects</a></li>112<li><a class="reference internal" href="#command-line-interface">Command-line interface</a><ul>113<li><a class="reference internal" href="#command-line-options">Command-line options</a></li>114</ul>115</li>116<li><a class="reference internal" href="#decompression-pitfalls">Decompression pitfalls</a><ul>117<li><a class="reference internal" href="#from-file-itself">From file itself</a></li>118<li><a class="reference internal" href="#file-system-limitations">File system limitations</a></li>119<li><a class="reference internal" href="#resources-limitations">Resources limitations</a></li>120<li><a class="reference internal" href="#interruption">Interruption</a></li>121<li><a class="reference internal" href="#default-behaviors-of-extraction">Default behaviors of extraction</a></li>122</ul>123</li>124</ul>125</li>126</ul>127 128 </div>129 <div>130 <h4>Previous topic</h4>131 <p class="topless"><a href="lzma.html"132 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>133 </div>134 <div>135 <h4>Next topic</h4>136 <p class="topless"><a href="tarfile.html"137 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>138 </div>139 <script>140 document.addEventListener('DOMContentLoaded', () => {141 const title = document.querySelector('meta[property="og:title"]').content;142 const elements = document.querySelectorAll('.improvepage');143 const pageurl = window.location.href.split('?')[0];144 elements.forEach(element => {145 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));146 url.searchParams.set('pagetitle', title);147 url.searchParams.set('pageurl', pageurl);148 url.searchParams.set('pagesource', "library/zipfile.rst");149 element.href = url.toString();150 });151 });152 </script>153 <div role="note" aria-label="source link">154 <h3>This page</h3>155 <ul class="this-page-menu">156 <li><a href="../bugs.html">Report a bug</a></li>157 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>158 <li>159 <a href="https://github.com/python/cpython/blob/main/Doc/library/zipfile.rst?plain=1"160 rel="nofollow">Show source161 </a>162 </li>163 164 </ul>165 </div>166 </nav>167 </div>168</div>169 170 171 <div class="related" role="navigation" aria-label="Related">172 <h3>Navigation</h3>173 <ul>174 <li class="right" style="margin-right: 10px">175 <a href="../genindex.html" title="General Index"176 accesskey="I">index</a></li>177 <li class="right" >178 <a href="../py-modindex.html" title="Python Module Index"179 >modules</a> |</li>180 <li class="right" >181 <a href="tarfile.html" title="tarfile — Read and write tar archive files"182 accesskey="N">next</a> |</li>183 <li class="right" >184 <a href="lzma.html" title="lzma — Compression using the LZMA algorithm"185 accesskey="P">previous</a> |</li>186 187 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>188 <li><a href="https://www.python.org/">Python</a> »</li>189 <li class="switchers">190 <div class="language_switcher_placeholder"></div>191 <div class="version_switcher_placeholder"></div>192 </li>193 <li>194 195 </li>196 <li id="cpython-language-and-version">197 <a href="../index.html">3.15.0a6 Documentation</a> »198 </li>199 200 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>201 <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">Data Compression and Archiving</a> »</li>202 <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>203 <li class="right">204 205 206 <div class="inline-search" role="search">207 <form class="inline-search" action="../search.html" method="get">208 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">209 <input type="submit" value="Go">210 </form>211 </div>212 |213 </li>214 <li class="right">215<label class="theme-selector-label">216 Theme217 <select class="theme-selector" oninput="activateTheme(this.value)">218 <option value="auto" selected>Auto</option>219 <option value="light">Light</option>220 <option value="dark">Dark</option>221 </select>222</label> |</li>223 224 </ul>225 </div> 226 227 <div class="document">228 <div class="documentwrapper">229 <div class="bodywrapper">230 <div class="body" role="main">231 232 <section id="module-zipfile">233<span id="zipfile-work-with-zip-archives"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> — Work with ZIP archives<a class="headerlink" href="#module-zipfile" title="Link to this heading">¶</a></h1>234<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/zipfile/">Lib/zipfile/</a></p>235<hr class="docutils" />236<p>The ZIP file format is a common archive and compression standard. This module237provides tools to create, read, write, append, and list a ZIP file. Any238advanced use of this module will require an understanding of the format, as239defined in <a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a>.</p>240<p>This module does not handle multipart ZIP files.241It can handle ZIP files that use the ZIP64 extensions242(that is ZIP files that are more than 4 GiB in size). It supports243decryption of encrypted files in ZIP archives, but it cannot244create an encrypted file. Decryption is extremely slow as it is245implemented in native Python rather than C.</p>246<p>Handling compressed archives requires <a class="reference internal" href="../glossary.html#term-optional-module"><span class="xref std std-term">optional modules</span></a>247such as <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>, <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>, and <a class="reference internal" href="compression.zstd.html#module-compression.zstd" title="compression.zstd: Low-level interface to compression and decompression routines in the zstd library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compression.zstd</span></code></a>.248If any of them are missing from your copy of CPython,249look for documentation from your distributor (that is,250whoever provided Python to you).251If you are the distributor, see <a class="reference internal" href="../using/configure.html#optional-module-requirements"><span class="std std-ref">Requirements for optional modules</span></a>.</p>252<p>The module defines the following items:</p>253<dl class="py exception">254<dt class="sig sig-object py" id="zipfile.BadZipFile">255<em class="property"><span class="k"><span class="pre">exception</span></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>256<dd><p>The error raised for bad ZIP files.</p>257<div class="versionadded">258<p><span class="versionmodified added">Added in version 3.2.</span></p>259</div>260</dd></dl>261 262<dl class="py exception">263<dt class="sig sig-object py" id="zipfile.BadZipfile">264<em class="property"><span class="k"><span class="pre">exception</span></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>265<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>266<div class="deprecated">267<p><span class="versionmodified deprecated">Deprecated since version 3.2.</span></p>268</div>269</dd></dl>270 271<dl class="py exception">272<dt class="sig sig-object py" id="zipfile.LargeZipFile">273<em class="property"><span class="k"><span class="pre">exception</span></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>274<dd><p>The error raised when a ZIP file would require ZIP64 functionality but that has275not been enabled.</p>276</dd></dl>277 278<dl class="py class">279<dt class="sig sig-object py">280<em class="property"><span class="k"><span class="pre">class</span></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>281<dd><p>The class for reading and writing ZIP files. See section282<a class="reference internal" href="#zipfile-objects"><span class="std std-ref">ZipFile objects</span></a> for constructor details.</p>283</dd></dl>284 285<dl class="py class">286<dt class="sig sig-object py">287<em class="property"><span class="k"><span class="pre">class</span></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>288<dd><p>Class that implements a subset of the interface provided by289<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 full290<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>291<div class="versionadded">292<p><span class="versionmodified added">Added in version 3.8.</span></p>293</div>294</dd></dl>295 296<dl class="py class">297<dt class="sig sig-object py">298<em class="property"><span class="k"><span class="pre">class</span></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>299<dd><p>Class for creating ZIP archives containing Python libraries.</p>300</dd></dl>301 302<dl class="py class">303<dt class="sig sig-object py" id="zipfile.ZipInfo">304<em class="property"><span class="k"><span class="pre">class</span></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>305<dd><p>Class used to represent information about a member of an archive. Instances306of 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>307methods 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 <code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> module308will not need to create these, but only use those created by this309module. <em>filename</em> should be the full name of the archive member, and310<em>date_time</em> should be a tuple containing six fields which describe the time311of the last modification to the file; the fields are described in section312<a class="reference internal" href="#zipinfo-objects"><span class="std std-ref">ZipInfo objects</span></a>.</p>313<div class="versionchanged">314<p><span class="versionmodified changed">Changed in version 3.13: </span>A public <code class="xref py py-attr docutils literal notranslate"><span class="pre">compress_level</span></code> attribute has been added to expose the315formerly protected <code class="xref py py-attr docutils literal notranslate"><span class="pre">_compresslevel</span></code>. The older protected name316continues to work as a property for backwards compatibility.</p>317</div>318<dl class="py method">319<dt class="sig sig-object py" id="zipfile.ZipInfo._for_archive">320<span class="sig-name descname"><span class="pre">_for_archive</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">archive</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.ZipInfo._for_archive" title="Link to this definition">¶</a></dt>321<dd><p>Resolve the date_time, compression attributes, and external attributes322to suitable defaults as used by <a class="reference internal" href="#zipfile.ZipFile.writestr" title="zipfile.ZipFile.writestr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ZipFile.writestr()</span></code></a>.</p>323<p>Returns self for chaining.</p>324<div class="versionadded">325<p><span class="versionmodified added">Added in version 3.14.</span></p>326</div>327</dd></dl>328 329</dd></dl>330 331<dl class="py function">332<dt class="sig sig-object py" id="zipfile.is_zipfile">333<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>334<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,335otherwise 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>336<div class="versionchanged">337<p><span class="versionmodified changed">Changed in version 3.1: </span>Support for file and file-like objects.</p>338</div>339</dd></dl>340 341<dl class="py data">342<dt class="sig sig-object py" id="zipfile.ZIP_STORED">343<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>344<dd><p>The numeric constant for an uncompressed archive member.</p>345</dd></dl>346 347<dl class="py data">348<dt class="sig sig-object py" id="zipfile.ZIP_DEFLATED">349<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>350<dd><p>The numeric constant for the usual ZIP compression method. This requires the351<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>352</dd></dl>353 354<dl class="py data">355<dt class="sig sig-object py" id="zipfile.ZIP_BZIP2">356<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>357<dd><p>The numeric constant for the BZIP2 compression method. This requires the358<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>359<div class="versionadded">360<p><span class="versionmodified added">Added in version 3.3.</span></p>361</div>362</dd></dl>363 364<dl class="py data">365<dt class="sig sig-object py" id="zipfile.ZIP_LZMA">366<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>367<dd><p>The numeric constant for the LZMA compression method. This requires the368<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>369<div class="versionadded">370<p><span class="versionmodified added">Added in version 3.3.</span></p>371</div>372</dd></dl>373 374<dl class="py data">375<dt class="sig sig-object py" id="zipfile.ZIP_ZSTANDARD">376<span class="sig-prename descclassname"><span class="pre">zipfile.</span></span><span class="sig-name descname"><span class="pre">ZIP_ZSTANDARD</span></span><a class="headerlink" href="#zipfile.ZIP_ZSTANDARD" title="Link to this definition">¶</a></dt>377<dd><p>The numeric constant for Zstandard compression. This requires the378<a class="reference internal" href="compression.zstd.html#module-compression.zstd" title="compression.zstd: Low-level interface to compression and decompression routines in the zstd library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compression.zstd</span></code></a> module.</p>379<div class="admonition note">380<p class="admonition-title">Note</p>381<p>In APPNOTE 6.3.7, the method ID <code class="docutils literal notranslate"><span class="pre">20</span></code> was assigned to Zstandard382compression. This was changed in APPNOTE 6.3.8 to method ID <code class="docutils literal notranslate"><span class="pre">93</span></code> to383avoid conflicts, with method ID <code class="docutils literal notranslate"><span class="pre">20</span></code> being deprecated. For384compatibility, the <code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> module reads both method IDs but will385only write data with method ID <code class="docutils literal notranslate"><span class="pre">93</span></code>.</p>386</div>387<div class="versionadded">388<p><span class="versionmodified added">Added in version 3.14.</span></p>389</div>390</dd></dl>391 392<div class="admonition note">393<p class="admonition-title">Note</p>394<p>The ZIP file format specification has included support for bzip2 compression395since 2001, for LZMA compression since 2006, and Zstandard compression since3962020. However, some tools (including older Python releases) do not support397these compression methods, and may either refuse to process the ZIP file398altogether, or fail to extract individual files.</p>399</div>400<div class="admonition seealso">401<p class="admonition-title">See also</p>402<dl class="simple">403<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 and404algorithms used.</p>405</dd>406<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 development407libraries.</p>408</dd>409</dl>410</div>411<section id="zipfile-objects">412<span id="id1"></span><h2>ZipFile objects<a class="headerlink" href="#zipfile-objects" title="Link to this heading">¶</a></h2>413<dl class="py class">414<dt class="sig sig-object py" id="zipfile.ZipFile">415<em class="property"><span class="k"><span class="pre">class</span></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="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></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>416<dd><p>Open a ZIP file, where <em>file</em> can be a path to a file (a string), a417file-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>418<p>The <em>mode</em> parameter should be <code class="docutils literal notranslate"><span class="pre">'r'</span></code> to read an existing419file, <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 an420existing file, or <code class="docutils literal notranslate"><span class="pre">'x'</span></code> to exclusively create and write a new file.421If <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">'x'</span></code> and <em>file</em> refers to an existing file,422a <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.423If <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">'a'</span></code> and <em>file</em> refers to an existing ZIP424file, then additional files are added to it. If <em>file</em> does not refer to a425ZIP file, then a new ZIP archive is appended to the file. This is meant for426adding a ZIP archive to another file (such as <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code>). If427<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.428If <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>429<p><em>compression</em> is the ZIP compression method to use when writing the archive,430and 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>,431<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>, <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>, or <a class="reference internal" href="#zipfile.ZIP_ZSTANDARD" title="zipfile.ZIP_ZSTANDARD"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_ZSTANDARD</span></code></a>;432unrecognized 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. If433<code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_DEFLATED</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_BZIP2</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_LZMA</span></code>, or434<code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_ZSTANDARD</span></code> is specified but the corresponding module435(<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>, <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>, or <a class="reference internal" href="compression.zstd.html#module-compression.zstd" title="compression.zstd: Low-level interface to compression and decompression routines in the zstd library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compression.zstd</span></code></a>) is not436available, <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 <code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_STORED</span></code>.</p>437<p>If <em>allowZip64</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default) zipfile will create ZIP files that438use the ZIP64 extensions when the zipfile is larger than 4 GiB. If it is439<code class="docutils literal notranslate"><span class="pre">false</span></code> <code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> will raise an exception when the ZIP file would440require ZIP64 extensions.</p>441<p>The <em>compresslevel</em> parameter controls the compression level to use when442writing files to the archive.443When 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.444When 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 accepted445(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).446When 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 accepted447(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).448When using <a class="reference internal" href="#zipfile.ZIP_ZSTANDARD" title="zipfile.ZIP_ZSTANDARD"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_ZSTANDARD</span></code></a> integers <code class="docutils literal notranslate"><span class="pre">-131072</span></code> through <code class="docutils literal notranslate"><span class="pre">22</span></code> are449commonly accepted (see450<a class="reference internal" href="compression.zstd.html#compression.zstd.CompressionParameter.compression_level" title="compression.zstd.CompressionParameter.compression_level"><code class="xref py py-attr docutils literal notranslate"><span class="pre">CompressionParameter.compression_level</span></code></a>451for more on retrieving valid values and their meaning).</p>452<p>The <em>strict_timestamps</em> argument, when set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, allows to453zip files older than 1980-01-01 at the cost of setting the454timestamp to 1980-01-01.455Similar behavior occurs with files newer than 2107-12-31,456the timestamp is also set to the limit.</p>457<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,458which will be used to decode metadata such as the names of members and ZIP459comments.</p>460<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 then461<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 appropriate462ZIP structures for an empty archive will be written to the file.</p>463<p>ZipFile is also a context manager and therefore supports the464<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 the465<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>466<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">'spam.zip'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">myzip</span><span class="p">:</span>467 <span class="n">myzip</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'eggs.txt'</span><span class="p">)</span>468</pre></div>469</div>470<div class="admonition note">471<p class="admonition-title">Note</p>472<p><em>metadata_encoding</em> is an instance-wide setting for the ZipFile.473It is not possible to set this on a per-member basis.</p>474<p>This attribute is a workaround for legacy implementations which produce475archives with names in the current locale encoding or code page (mostly476on Windows). According to the .ZIP standard, the encoding of metadata477may be specified to be either IBM code page (default) or UTF-8 by a flag478in the archive header.479That flag takes precedence over <em>metadata_encoding</em>, which is480a Python-specific extension.</p>481</div>482<div class="versionchanged">483<p><span class="versionmodified changed">Changed in version 3.2: </span>Added the ability to use <code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code> as a context manager.</p>484</div>485<div class="versionchanged">486<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>487</div>488<div class="versionchanged">489<p><span class="versionmodified changed">Changed in version 3.4: </span>ZIP64 extensions are enabled by default.</p>490</div>491<div class="versionchanged">492<p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for writing to unseekable streams.493Added support for the <code class="docutils literal notranslate"><span class="pre">'x'</span></code> mode.</p>494</div>495<div class="versionchanged">496<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 unrecognized497compression values.</p>498</div>499<div class="versionchanged">500<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>501</div>502<div class="versionchanged">503<p><span class="versionmodified changed">Changed in version 3.7: </span>Add the <em>compresslevel</em> parameter.</p>504</div>505<div class="versionchanged">506<p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>strict_timestamps</em> keyword-only parameter.</p>507</div>508<div class="versionchanged">509<p><span class="versionmodified changed">Changed in version 3.11: </span>Added support for specifying member name encoding for reading510metadata in the zipfile’s directory and file headers.</p>511</div>512</dd></dl>513 514<dl class="py method">515<dt class="sig sig-object py" id="zipfile.ZipFile.close">516<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>517<dd><p>Close the archive file. You must call <code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code> before exiting your program518or essential records will not be written.</p>519</dd></dl>520 521<dl class="py method">522<dt class="sig sig-object py" id="zipfile.ZipFile.getinfo">523<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>524<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 member525<em>name</em>. Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">getinfo()</span></code> for a name not currently contained in the526archive 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>527</dd></dl>528 529<dl class="py method">530<dt class="sig sig-object py" id="zipfile.ZipFile.infolist">531<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>532<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 the533archive. The objects are in the same order as their entries in the actual ZIP534file on disk if an existing archive was opened.</p>535</dd></dl>536 537<dl class="py method">538<dt class="sig sig-object py" id="zipfile.ZipFile.namelist">539<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>540<dd><p>Return a list of archive members by name.</p>541</dd></dl>542 543<dl class="py method">544<dt class="sig sig-object py" id="zipfile.ZipFile.open">545<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="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></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>546<dd><p>Access a member of the archive as a binary file-like object. <em>name</em>547can 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>548object. The <em>mode</em> parameter, if included, must be <code class="docutils literal notranslate"><span class="pre">'r'</span></code> (the default)549or <code class="docutils literal notranslate"><span class="pre">'w'</span></code>. <em>pwd</em> is the password used to decrypt encrypted ZIP files as a550<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>551<p><code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code> is also a context manager and therefore supports the552<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>553<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">'spam.zip'</span><span class="p">)</span> <span class="k">as</span> <span class="n">myzip</span><span class="p">:</span>554 <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">'eggs.txt'</span><span class="p">)</span> <span class="k">as</span> <span class="n">myfile</span><span class="p">:</span>555 <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>556</pre></div>557</div>558<p>With <em>mode</em> <code class="docutils literal notranslate"><span class="pre">'r'</span></code> the file-like object559(<code class="docutils literal notranslate"><span class="pre">ZipExtFile</span></code>) is read-only and provides the following methods:560<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>,561<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>,562<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>.563These objects can operate independently of the ZipFile.</p>564<p>With <code class="docutils literal notranslate"><span class="pre">mode='w'</span></code>, a writable file handle is returned, which supports the565<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,566attempting to read or write other files in the ZIP file will raise a567<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>568<p>In both cases the file-like object has also attributes <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code>,569which is equivalent to the name of a file within the archive, and570<code class="xref py py-attr docutils literal notranslate"><span class="pre">mode</span></code>, which is <code class="docutils literal notranslate"><span class="pre">'rb'</span></code> or <code class="docutils literal notranslate"><span class="pre">'wb'</span></code> depending on the input mode.</p>571<p>When writing a file, if the file size is not known in advance but may exceed5722 GiB, pass <code class="docutils literal notranslate"><span class="pre">force_zip64=True</span></code> to ensure that the header format is573capable of supporting large files. If the file size is known in advance,574construct 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, and575use that as the <em>name</em> parameter.</p>576<div class="admonition note">577<p class="admonition-title">Note</p>578<p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code>, <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 filename579or 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 a580ZIP file that contains members with duplicate names.</p>581</div>582<div class="versionchanged">583<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 reading584compressed 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>585</div>586<div class="versionchanged">587<p><span class="versionmodified changed">Changed in version 3.6: </span><code class="xref py py-meth docutils literal notranslate"><span class="pre">ZipFile.open()</span></code> can now be used to write files into the archive with the588<code class="docutils literal notranslate"><span class="pre">mode='w'</span></code> option.</p>589</div>590<div class="versionchanged">591<p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">open()</span></code> 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>.592Previously, 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>593</div>594<div class="versionchanged">595<p><span class="versionmodified changed">Changed in version 3.13: </span>Added attributes <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">mode</span></code> for the writeable596file-like object.597The value of the <code class="xref py py-attr docutils literal notranslate"><span class="pre">mode</span></code> attribute for the readable file-like598object was changed from <code class="docutils literal notranslate"><span class="pre">'r'</span></code> to <code class="docutils literal notranslate"><span class="pre">'rb'</span></code>.</p>599</div>600</dd></dl>601 602<dl class="py method">603<dt class="sig sig-object py" id="zipfile.ZipFile.extract">604<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>605<dd><p>Extract a member from the archive to the current working directory; <em>member</em>606must 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 is607extracted as accurately as possible. <em>path</em> specifies a different directory608to extract to. <em>member</em> can be a filename or a <code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code> object.609<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>610<p>Returns the normalized path created (a directory or new file).</p>611<div class="admonition note">612<p class="admonition-title">Note</p>613<p>If a member filename is an absolute path, a drive/UNC sharepoint and614leading (back)slashes will be stripped, e.g.: <code class="docutils literal notranslate"><span class="pre">///foo/bar</span></code> becomes615<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.616And all <code class="docutils literal notranslate"><span class="pre">".."</span></code> components in a member filename will be removed, e.g.:617<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 illegal618characters (<code class="docutils literal notranslate"><span class="pre">:</span></code>, <code class="docutils literal notranslate"><span class="pre"><</span></code>, <code class="docutils literal notranslate"><span class="pre">></span></code>, <code class="docutils literal notranslate"><span class="pre">|</span></code>, <code class="docutils literal notranslate"><span class="pre">"</span></code>, <code class="docutils literal notranslate"><span class="pre">?</span></code>, and <code class="docutils literal notranslate"><span class="pre">*</span></code>)619replaced by underscore (<code class="docutils literal notranslate"><span class="pre">_</span></code>).</p>620</div>621<div class="versionchanged">622<p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code> on a closed ZipFile will raise a623<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>624</div>625<div class="versionchanged">626<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>627</div>628</dd></dl>629 630<dl class="py method">631<dt class="sig sig-object py" id="zipfile.ZipFile.extractall">632<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>633<dd><p>Extract all members from the archive to the current working directory. <em>path</em>634specifies a different directory to extract to. <em>members</em> is optional and must635be 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 password636used 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>637<div class="admonition warning">638<p class="admonition-title">Warning</p>639<p>Never extract archives from untrusted sources without prior inspection.640It is possible that files are created outside of <em>path</em>, e.g. members641that have absolute filenames starting with <code class="docutils literal notranslate"><span class="pre">"/"</span></code> or filenames with two642dots <code class="docutils literal notranslate"><span class="pre">".."</span></code>. This module attempts to prevent that.643See <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>644</div>645<div class="versionchanged">646<p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code> on a closed ZipFile will raise a647<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>648</div>649<div class="versionchanged">650<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>651</div>652</dd></dl>653 654<dl class="py method">655<dt class="sig sig-object py" id="zipfile.ZipFile.printdir">656<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>657<dd><p>Print a table of contents for the archive to <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>.</p>658</dd></dl>659 660<dl class="py method">661<dt class="sig sig-object py" id="zipfile.ZipFile.setpassword">662<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>663<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>664</dd></dl>665 666<dl class="py method">667<dt class="sig sig-object py" id="zipfile.ZipFile.read">668<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>669<dd><p>Return the bytes of the file <em>name</em> in the archive. <em>name</em> is the name of the670file 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 for671read 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>672object 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>.673Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> on a ZipFile that uses a compression method other than674<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>,675<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>, or <a class="reference internal" href="#zipfile.ZIP_ZSTANDARD" title="zipfile.ZIP_ZSTANDARD"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZIP_ZSTANDARD</span></code></a> will raise a676<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 be raised if the677corresponding compression module is not available.</p>678<div class="versionchanged">679<p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> 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>.680Previously, 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>681</div>682</dd></dl>683 684<dl class="py method">685<dt class="sig sig-object py" id="zipfile.ZipFile.testzip">686<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>687<dd><p>Read all the files in the archive and check their CRC’s and file headers.688Return the name of the first bad file, or else return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>689<div class="versionchanged">690<p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">testzip()</span></code> on a closed ZipFile will raise a691<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>692</div>693</dd></dl>694 695<dl class="py method">696<dt class="sig sig-object py" id="zipfile.ZipFile.write">697<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>698<dd><p>Write the file named <em>filename</em> to the archive, giving it the archive name699<em>arcname</em> (by default, this will be the same as <em>filename</em>, but without a drive700letter and with leading path separators removed). If given, <em>compress_type</em>701overrides the value given for the <em>compression</em> parameter to the constructor for702the new entry. Similarly, <em>compresslevel</em> will override the constructor if703given.704The 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>705<div class="admonition note">706<p class="admonition-title">Note</p>707<p>The ZIP file standard historically did not specify a metadata encoding,708but strongly recommended CP437 (the original IBM PC encoding) for709interoperability. Recent versions allow use of UTF-8 (only). In this710module, UTF-8 will automatically be used to write the member names if711they contain any non-ASCII characters. It is not possible to write712member names in any encoding other than ASCII or UTF-8.</p>713</div>714<div class="admonition note">715<p class="admonition-title">Note</p>716<p>Archive names should be relative to the archive root, that is, they should not717start with a path separator.</p>718</div>719<div class="admonition note">720<p class="admonition-title">Note</p>721<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 null722byte, the name of the file in the archive will be truncated at the null byte.</p>723</div>724<div class="admonition note">725<p class="admonition-title">Note</p>726<p>A leading slash in the filename may lead to the archive being impossible to727open in some zip programs on Windows systems.</p>728</div>729<div class="versionchanged">730<p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> on a ZipFile created with mode <code class="docutils literal notranslate"><span class="pre">'r'</span></code> or731a 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,732a <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>733</div>734</dd></dl>735 736<dl class="py method">737<dt class="sig sig-object py" id="zipfile.ZipFile.writestr">738<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>739<dd><p>Write a file into the archive. The contents is <em>data</em>, which may be either740a <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 <code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code>,741it is encoded as UTF-8 first. <em>zinfo_or_arcname</em> is either the file742name 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’s743an instance, at least the filename, date, and time must be given. If it’s a744name, the date and time is set to the current date and time.745The 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>746<p>If given, <em>compress_type</em> overrides the value given for the <em>compression</em>747parameter to the constructor for the new entry, or in the <em>zinfo_or_arcname</em>748(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> will749override the constructor if given.</p>750<div class="admonition note">751<p class="admonition-title">Note</p>752<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,753the compression method used will be that specified in the <em>compress_type</em>754member of the given <code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code> instance. By default, the755<code class="xref py py-class docutils literal notranslate"><span class="pre">ZipInfo</span></code> 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>756</div>757<div class="versionchanged">758<p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>compress_type</em> argument.</p>759</div>760<div class="versionchanged">761<p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">writestr()</span></code> on a ZipFile created with mode <code class="docutils literal notranslate"><span class="pre">'r'</span></code> or762a 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,763a <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>764</div>765</dd></dl>766 767<dl class="py method">768<dt class="sig sig-object py" id="zipfile.ZipFile.mkdir">769<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>770<dd><p>Create a directory inside the archive. If <em>zinfo_or_directory</em> is a string,771a directory is created inside the archive with the mode that is specified in772the <em>mode</em> argument. If, however, <em>zinfo_or_directory</em> is773a <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>774<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>775<div class="versionadded">776<p><span class="versionmodified added">Added in version 3.11.</span></p>777</div>778</dd></dl>779 780<p>The following data attributes are also available:</p>781<dl class="py attribute">782<dt class="sig sig-object py" id="zipfile.ZipFile.filename">783<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>784<dd><p>Name of the ZIP file.</p>785</dd></dl>786 787<dl class="py attribute">788<dt class="sig sig-object py" id="zipfile.ZipFile.debug">789<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>790<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, no791output) to <code class="docutils literal notranslate"><span class="pre">3</span></code> (the most output). Debugging information is written to792<code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>.</p>793</dd></dl>794 795<dl class="py attribute">796<dt class="sig sig-object py" id="zipfile.ZipFile.comment">797<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>798<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.799If assigning a comment to a800<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>,801it should be no longer than 65535 bytes. Comments longer than this will be802truncated.</p>803</dd></dl>804 805</section>806<section id="path-objects">807<span id="id2"></span><h2>Path objects<a class="headerlink" href="#path-objects" title="Link to this heading">¶</a></h2>808<dl class="py class">809<dt class="sig sig-object py" id="zipfile.Path">810<em class="property"><span class="k"><span class="pre">class</span></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>811<dd><p>Construct a Path object from a <code class="docutils literal notranslate"><span class="pre">root</span></code> zipfile (which may be a812<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 to813the <code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code> constructor).</p>814<p><code class="docutils literal notranslate"><span class="pre">at</span></code> specifies the location of this Path within the zipfile,815e.g. ‘dir/file.txt’, ‘dir/’, or ‘’. Defaults to the empty string,816indicating the root.</p>817<div class="admonition note">818<p class="admonition-title">Note</p>819<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code> class does not sanitize filenames within the ZIP archive. Unlike820the <a class="reference internal" href="#zipfile.ZipFile.extract" title="zipfile.ZipFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ZipFile.extract()</span></code></a> and <a class="reference internal" href="#zipfile.ZipFile.extractall" title="zipfile.ZipFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ZipFile.extractall()</span></code></a> methods, it is the821caller’s responsibility to validate or sanitize filenames to prevent path traversal822vulnerabilities (e.g., filenames containing “..” or absolute paths). When handling823untrusted archives, consider resolving filenames using <a class="reference internal" href="os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.abspath()</span></code></a>824and checking against the target directory with <a class="reference internal" href="os.path.html#os.path.commonpath" title="os.path.commonpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.commonpath()</span></code></a>.</p>825</div>826</dd></dl>827 828<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>829objects:</p>830<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>831<dl class="py attribute">832<dt class="sig sig-object py" id="zipfile.Path.name">833<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>834<dd><p>The final path component.</p>835</dd></dl>836 837<dl class="py method">838<dt class="sig sig-object py" id="zipfile.Path.open">839<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>840<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.841Allows opening for read or write, text or binary842through supported modes: ‘r’, ‘w’, ‘rb’, ‘wb’.843Positional and keyword arguments are passed through to844<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 and845ignored otherwise.846<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 to847<code class="xref py py-meth docutils literal notranslate"><span class="pre">ZipFile.open()</span></code>.</p>848<div class="versionchanged">849<p><span class="versionmodified changed">Changed in version 3.9: </span>Added support for text and binary modes for open. Default850mode is now text.</p>851</div>852<div class="versionchanged">853<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 argument854without 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 to855be compatible with unpatched 3.10 and 3.11 versions must pass all856<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>857</div>858</dd></dl>859 860<dl class="py method">861<dt class="sig sig-object py" id="zipfile.Path.iterdir">862<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>863<dd><p>Enumerate the children of the current directory.</p>864</dd></dl>865 866<dl class="py method">867<dt class="sig sig-object py" id="zipfile.Path.is_dir">868<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>869<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the current context references a directory.</p>870</dd></dl>871 872<dl class="py method">873<dt class="sig sig-object py" id="zipfile.Path.is_file">874<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>875<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the current context references a file.</p>876</dd></dl>877 878<dl class="py method">879<dt class="sig sig-object py" id="zipfile.Path.is_symlink">880<span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_symlink</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zipfile.Path.is_symlink" title="Link to this definition">¶</a></dt>881<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the current context references a symbolic link.</p>882<div class="versionadded">883<p><span class="versionmodified added">Added in version 3.12.</span></p>884</div>885<div class="versionchanged">886<p><span class="versionmodified changed">Changed in version 3.13: </span>Previously, <code class="docutils literal notranslate"><span class="pre">is_symlink</span></code> would unconditionally return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>887</div>888</dd></dl>889 890<dl class="py method">891<dt class="sig sig-object py" id="zipfile.Path.exists">892<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>893<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the current context references a file or894directory in the zip file.</p>895</dd></dl>896 897<dl class="py data">898<dt class="sig sig-object py" id="zipfile.Path.suffix">899<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>900<dd><p>The last dot-separated portion of the final component, if any.901This is commonly called the file extension.</p>902<div class="versionadded">903<p><span class="versionmodified added">Added in version 3.11: </span>Added <code class="xref py py-data docutils literal notranslate"><span class="pre">Path.suffix</span></code> property.</p>904</div>905</dd></dl>906 907<dl class="py data">908<dt class="sig sig-object py" id="zipfile.Path.stem">909<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>910<dd><p>The final path component, without its suffix.</p>911<div class="versionadded">912<p><span class="versionmodified added">Added in version 3.11: </span>Added <code class="xref py py-data docutils literal notranslate"><span class="pre">Path.stem</span></code> property.</p>913</div>914</dd></dl>915 916<dl class="py data">917<dt class="sig sig-object py" id="zipfile.Path.suffixes">918<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>919<dd><p>A list of the path’s suffixes, commonly called file extensions.</p>920<div class="versionadded">921<p><span class="versionmodified added">Added in version 3.11: </span>Added <code class="xref py py-data docutils literal notranslate"><span class="pre">Path.suffixes</span></code> property.</p>922</div>923</dd></dl>924 925<dl class="py method">926<dt class="sig sig-object py" id="zipfile.Path.read_text">927<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>928<dd><p>Read the current file as unicode text. Positional and929keyword arguments are passed through to930<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 is931implied by the context).</p>932<div class="versionchanged">933<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 argument934without 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 to935be compatible with unpatched 3.10 and 3.11 versions must pass all936<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>937</div>938</dd></dl>939 940<dl class="py method">941<dt class="sig sig-object py" id="zipfile.Path.read_bytes">942<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>943<dd><p>Read the current file as bytes.</p>944</dd></dl>945 946<dl class="py method">947<dt class="sig sig-object py" id="zipfile.Path.joinpath">948<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>949<dd><p>Return a new Path object with each of the <em>other</em> arguments950joined. The following are equivalent:</p>951<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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">'child'</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="s1">'grandchild'</span><span class="p">)</span>952<span class="gp">>>> </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">'child'</span><span class="p">,</span> <span class="s1">'grandchild'</span><span class="p">)</span>953<span class="gp">>>> </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">'child'</span> <span class="o">/</span> <span class="s1">'grandchild'</span>954</pre></div>955</div>956<div class="versionchanged">957<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 accepted958exactly one parameter.</p>959</div>960</dd></dl>961 962<p>The <a class="extlink-pypi reference external" href="https://pypi.org/project/zipp/">zipp</a> project provides backports963of the latest path object functionality to older Pythons. Use964<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 to965changes.</p>966</section>967<section id="pyzipfile-objects">968<span id="id3"></span><h2>PyZipFile objects<a class="headerlink" href="#pyzipfile-objects" title="Link to this heading">¶</a></h2>969<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 the970<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>971<dl class="py class">972<dt class="sig sig-object py" id="zipfile.PyZipFile">973<em class="property"><span class="k"><span class="pre">class</span></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>974<dd><div class="versionchanged">975<p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>optimize</em> parameter.</p>976</div>977<div class="versionchanged">978<p><span class="versionmodified changed">Changed in version 3.4: </span>ZIP64 extensions are enabled by default.</p>979</div>980<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>981<dl class="py method">982<dt class="sig sig-object py" id="zipfile.PyZipFile.writepy">983<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>984<dd><p>Search for files <code class="file docutils literal notranslate"><span class="pre">*.py</span></code> and add the corresponding file to the985archive.</p>986<p>If the <em>optimize</em> parameter to <code class="xref py py-class docutils literal notranslate"><span class="pre">PyZipFile</span></code> was not given or <code class="docutils literal notranslate"><span class="pre">-1</span></code>,987the corresponding file is a <code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code> file, compiling if necessary.</p>988<p>If the <em>optimize</em> parameter to <code class="xref py py-class docutils literal notranslate"><span class="pre">PyZipFile</span></code> was <code class="docutils literal notranslate"><span class="pre">0</span></code>, <code class="docutils literal notranslate"><span class="pre">1</span></code> or989<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>) are990added to the archive, compiling if necessary.</p>991<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>, and992just the (corresponding <code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code>) file is added at the top level993(no path information). If <em>pathname</em> is a file that does not end with994<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,995and the directory is not a package directory, then all the files996<code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code> are added at the top level. If the directory is a997package directory, then all <code class="file docutils literal notranslate"><span class="pre">*.pyc</span></code> are added under the package998name as a file path, and if any subdirectories are package directories,999all of these are added recursively in sorted order.</p>1000<p><em>basename</em> is intended for internal use only.</p>1001<p><em>filterfunc</em>, if given, must be a function taking a single string1002argument. It will be passed each path (including each individual full1003file path) before it is added to the archive. If <em>filterfunc</em> returns a1004false value, the path will not be added, and if it is a directory its1005contents will be ignored. For example, if our test files are all either1006in <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 a1007<em>filterfunc</em> to exclude them:</p>1008<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">zf</span> <span class="o">=</span> <span class="n">PyZipFile</span><span class="p">(</span><span class="s1">'myprog.zip'</span><span class="p">)</span>1009<span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">notests</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>1010<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>1011<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">'test'</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">'test_'</span><span class="p">)))</span>1012<span class="gp">...</span>1013<span class="gp">>>> </span><span class="n">zf</span><span class="o">.</span><span class="n">writepy</span><span class="p">(</span><span class="s1">'myprog'</span><span class="p">,</span> <span class="n">filterfunc</span><span class="o">=</span><span class="n">notests</span><span class="p">)</span>1014</pre></div>1015</div>1016<p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">writepy()</span></code> method makes archives with file names like1017this:</p>1018<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>1019<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>1020<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>1021<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>1022<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>1023</pre></div>1024</div>1025<div class="versionchanged">1026<p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <em>filterfunc</em> parameter.</p>1027</div>1028<div class="versionchanged">1029<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>1030</div>1031<div class="versionchanged">1032<p><span class="versionmodified changed">Changed in version 3.7: </span>Recursion sorts directory entries.</p>1033</div>1034</dd></dl>1035 1036</dd></dl>1037 1038</section>1039<section id="zipinfo-objects">1040<span id="id4"></span><h2>ZipInfo objects<a class="headerlink" href="#zipinfo-objects" title="Link to this heading">¶</a></h2>1041<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> and1042<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 stores1043information about a single member of the ZIP archive.</p>1044<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 filesystem1045file:</p>1046<dl class="py method">1047<dt class="sig sig-object py" id="zipfile.ZipInfo.from_file">1048<em class="property"><span class="k"><span class="pre">classmethod</span></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="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></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>1049<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, in1050preparation for adding it to a zip file.</p>1051<p><em>filename</em> should be the path to a file or directory on the filesystem.</p>1052<p>If <em>arcname</em> is specified, it is used as the name within the archive.1053If <em>arcname</em> is not specified, the name will be the same as <em>filename</em>, but1054with any drive letter and leading path separators removed.</p>1055<p>The <em>strict_timestamps</em> argument, when set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, allows to1056zip files older than 1980-01-01 at the cost of setting the1057timestamp to 1980-01-01.1058Similar behavior occurs with files newer than 2107-12-31,1059the timestamp is also set to the limit.</p>1060<div class="versionadded">1061<p><span class="versionmodified added">Added in version 3.6.</span></p>1062</div>1063<div class="versionchanged">1064<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>1065</div>1066<div class="versionchanged">1067<p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>strict_timestamps</em> keyword-only parameter.</p>1068</div>1069</dd></dl>1070 1071<p>Instances have the following methods and attributes:</p>1072<dl class="py method">1073<dt class="sig sig-object py" id="zipfile.ZipInfo.is_dir">1074<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>1075<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if this archive member is a directory.</p>1076<p>This uses the entry’s name: directories should always end with <code class="docutils literal notranslate"><span class="pre">/</span></code>.</p>1077<div class="versionadded">1078<p><span class="versionmodified added">Added in version 3.6.</span></p>1079</div>1080</dd></dl>1081 1082<dl class="py attribute">1083<dt class="sig sig-object py" id="zipfile.ZipInfo.filename">1084<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>1085<dd><p>Name of the file in the archive.</p>1086</dd></dl>1087 1088<dl class="py attribute">1089<dt class="sig sig-object py" id="zipfile.ZipInfo.date_time">1090<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>1091<dd><p>The time and date of the last modification to the archive member. This is a1092tuple of six values representing the “last [modified] file time” and “last [modified] file date”1093fields from the ZIP file’s central directory.</p>1094<p>The tuple contains:</p>1095<table class="docutils align-default">1096<thead>1097<tr class="row-odd"><th class="head"><p>Index</p></th>1098<th class="head"><p>Value</p></th>1099</tr>1100</thead>1101<tbody>1102<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">0</span></code></p></td>1103<td><p>Year (>= 1980)</p></td>1104</tr>1105<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">1</span></code></p></td>1106<td><p>Month (one-based)</p></td>1107</tr>1108<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">2</span></code></p></td>1109<td><p>Day of month (one-based)</p></td>1110</tr>1111<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">3</span></code></p></td>1112<td><p>Hours (zero-based)</p></td>1113</tr>1114<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">4</span></code></p></td>1115<td><p>Minutes (zero-based)</p></td>1116</tr>1117<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">5</span></code></p></td>1118<td><p>Seconds (zero-based)</p></td>1119</tr>1120</tbody>1121</table>1122<div class="admonition note">1123<p class="admonition-title">Note</p>1124<p>The ZIP format supports multiple timestamp fields in different locations1125(central directory, extra fields for NTFS/UNIX systems, etc.). This attribute1126specifically returns the timestamp from the central directory. The central1127directory timestamp format in ZIP files does not support timestamps before11281980. While some extra field formats (such as UNIX timestamps) can represent1129earlier dates, this attribute only returns the central directory timestamp.</p>1130<p>The central directory timestamp is interpreted as representing local1131time, rather than UTC time, to match the behavior of other zip tools.</p>1132</div>1133</dd></dl>1134 1135<dl class="py attribute">1136<dt class="sig sig-object py" id="zipfile.ZipInfo.compress_type">1137<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>1138<dd><p>Type of compression for the archive member.</p>1139</dd></dl>1140 1141<dl class="py attribute">1142<dt class="sig sig-object py" id="zipfile.ZipInfo.comment">1143<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>1144<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>1145</dd></dl>1146 1147<dl class="py attribute">1148<dt class="sig sig-object py" id="zipfile.ZipInfo.extra">1149<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>1150<dd><p>Expansion field data. The <a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a> contains1151some comments on the internal structure of the data contained in this1152<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>1153</dd></dl>1154 1155<dl class="py attribute">1156<dt class="sig sig-object py" id="zipfile.ZipInfo.create_system">1157<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>1158<dd><p>System which created ZIP archive.</p>1159</dd></dl>1160 1161<dl class="py attribute">1162<dt class="sig sig-object py" id="zipfile.ZipInfo.create_version">1163<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>1164<dd><p>PKZIP version which created ZIP archive.</p>1165</dd></dl>1166 1167<dl class="py attribute">1168<dt class="sig sig-object py" id="zipfile.ZipInfo.extract_version">1169<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>1170<dd><p>PKZIP version needed to extract archive.</p>1171</dd></dl>1172 1173<dl class="py attribute">1174<dt class="sig sig-object py" id="zipfile.ZipInfo.reserved">1175<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>1176<dd><p>Must be zero.</p>1177</dd></dl>1178 1179<dl class="py attribute">1180<dt class="sig sig-object py" id="zipfile.ZipInfo.flag_bits">1181<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>1182<dd><p>ZIP flag bits.</p>1183</dd></dl>1184 1185<dl class="py attribute">1186<dt class="sig sig-object py" id="zipfile.ZipInfo.volume">1187<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>1188<dd><p>Volume number of file header.</p>1189</dd></dl>1190 1191<dl class="py attribute">1192<dt class="sig sig-object py" id="zipfile.ZipInfo.internal_attr">1193<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>1194<dd><p>Internal attributes.</p>1195</dd></dl>1196 1197<dl class="py attribute">1198<dt class="sig sig-object py" id="zipfile.ZipInfo.external_attr">1199<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>1200<dd><p>External file attributes.</p>