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

1805 lines
178KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="tarfile — Read and write tar archive files" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/tarfile.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/tarfile.py The tarfile module makes it possible to read and write tar archives, including those using gzip, bz2 and lzma compression. Use the zipfile module to read or write.zip fi..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/tarfile.py The tarfile module makes it possible to read and write tar archives, including those using gzip, bz2 and lzma compression. Use the zipfile module to read or write.zip fi..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>tarfile — Read and write tar archive files &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="File Formats" href="fileformats.html" />
  33. <link rel="prev" title="zipfile — Work with ZIP archives" href="zipfile.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/tarfile.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code> — Read and write tar archive files</a><ul>
  86. <li><a class="reference internal" href="#tarfile-objects">TarFile Objects</a></li>
  87. <li><a class="reference internal" href="#tarinfo-objects">TarInfo Objects</a></li>
  88. <li><a class="reference internal" href="#extraction-filters">Extraction filters</a><ul>
  89. <li><a class="reference internal" href="#default-named-filters">Default named filters</a></li>
  90. <li><a class="reference internal" href="#filter-errors">Filter errors</a></li>
  91. <li><a class="reference internal" href="#hints-for-further-verification">Hints for further verification</a></li>
  92. <li><a class="reference internal" href="#supporting-older-python-versions">Supporting older Python versions</a></li>
  93. <li><a class="reference internal" href="#stateful-extraction-filter-example">Stateful extraction filter example</a></li>
  94. </ul>
  95. </li>
  96. <li><a class="reference internal" href="#command-line-interface">Command-Line Interface</a><ul>
  97. <li><a class="reference internal" href="#command-line-options">Command-line options</a></li>
  98. </ul>
  99. </li>
  100. <li><a class="reference internal" href="#examples">Examples</a></li>
  101. <li><a class="reference internal" href="#supported-tar-formats">Supported tar formats</a></li>
  102. <li><a class="reference internal" href="#unicode-issues">Unicode issues</a></li>
  103. </ul>
  104. </li>
  105. </ul>
  106. </div>
  107. <div>
  108. <h4>Previous topic</h4>
  109. <p class="topless"><a href="zipfile.html"
  110. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> — Work with ZIP archives</a></p>
  111. </div>
  112. <div>
  113. <h4>Next topic</h4>
  114. <p class="topless"><a href="fileformats.html"
  115. title="next chapter">File Formats</a></p>
  116. </div>
  117. <div role="note" aria-label="source link">
  118. <h3>This Page</h3>
  119. <ul class="this-page-menu">
  120. <li><a href="../bugs.html">Report a Bug</a></li>
  121. <li>
  122. <a href="https://github.com/python/cpython/blob/main/Doc/library/tarfile.rst"
  123. rel="nofollow">Show Source
  124. </a>
  125. </li>
  126. </ul>
  127. </div>
  128. </nav>
  129. </div>
  130. </div>
  131. <div class="related" role="navigation" aria-label="related navigation">
  132. <h3>Navigation</h3>
  133. <ul>
  134. <li class="right" style="margin-right: 10px">
  135. <a href="../genindex.html" title="General Index"
  136. accesskey="I">index</a></li>
  137. <li class="right" >
  138. <a href="../py-modindex.html" title="Python Module Index"
  139. >modules</a> |</li>
  140. <li class="right" >
  141. <a href="fileformats.html" title="File Formats"
  142. accesskey="N">next</a> |</li>
  143. <li class="right" >
  144. <a href="zipfile.html" title="zipfile — Work with ZIP archives"
  145. accesskey="P">previous</a> |</li>
  146. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  147. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  148. <li class="switchers">
  149. <div class="language_switcher_placeholder"></div>
  150. <div class="version_switcher_placeholder"></div>
  151. </li>
  152. <li>
  153. </li>
  154. <li id="cpython-language-and-version">
  155. <a href="../index.html">3.12.3 Documentation</a> &#187;
  156. </li>
  157. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  158. <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">Data Compression and Archiving</a> &#187;</li>
  159. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code> — Read and write tar archive files</a></li>
  160. <li class="right">
  161. <div class="inline-search" role="search">
  162. <form class="inline-search" action="../search.html" method="get">
  163. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  164. <input type="submit" value="Go" />
  165. </form>
  166. </div>
  167. |
  168. </li>
  169. <li class="right">
  170. <label class="theme-selector-label">
  171. Theme
  172. <select class="theme-selector" oninput="activateTheme(this.value)">
  173. <option value="auto" selected>Auto</option>
  174. <option value="light">Light</option>
  175. <option value="dark">Dark</option>
  176. </select>
  177. </label> |</li>
  178. </ul>
  179. </div>
  180. <div class="document">
  181. <div class="documentwrapper">
  182. <div class="bodywrapper">
  183. <div class="body" role="main">
  184. <section id="module-tarfile">
  185. <span id="tarfile-read-and-write-tar-archive-files"></span><h1><a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> — Read and write tar archive files<a class="headerlink" href="#module-tarfile" title="Link to this heading">¶</a></h1>
  186. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/tarfile.py">Lib/tarfile.py</a></p>
  187. <hr class="docutils" />
  188. <p>The <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> module makes it possible to read and write tar
  189. archives, including those using gzip, bz2 and lzma compression.
  190. Use the <a class="reference internal" href="zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> module to read or write <code class="file docutils literal notranslate"><span class="pre">.zip</span></code> files, or the
  191. higher-level functions in <a class="reference internal" href="shutil.html#archiving-operations"><span class="std std-ref">shutil</span></a>.</p>
  192. <p>Some facts and figures:</p>
  193. <ul class="simple">
  194. <li><p>reads and writes <a class="reference internal" href="gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</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> 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> compressed archives
  195. if the respective modules are available.</p></li>
  196. <li><p>read/write support for the POSIX.1-1988 (ustar) format.</p></li>
  197. <li><p>read/write support for the GNU tar format including <em>longname</em> and <em>longlink</em>
  198. extensions, read-only support for all variants of the <em>sparse</em> extension
  199. including restoration of sparse files.</p></li>
  200. <li><p>read/write support for the POSIX.1-2001 (pax) format.</p></li>
  201. <li><p>handles directories, regular files, hardlinks, symbolic links, fifos,
  202. character devices and block devices and is able to acquire and restore file
  203. information like timestamp, access permissions and owner.</p></li>
  204. </ul>
  205. <div class="versionchanged">
  206. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added support for <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>
  207. </div>
  208. <div class="versionchanged">
  209. <p><span class="versionmodified changed">Changed in version 3.12: </span>Archives are extracted using a <a class="reference internal" href="#tarfile-extraction-filter"><span class="std std-ref">filter</span></a>,
  210. which makes it possible to either limit surprising/dangerous features,
  211. or to acknowledge that they are expected and the archive is fully trusted.
  212. By default, archives are fully trusted, but this default is deprecated
  213. and slated to change in Python 3.14.</p>
  214. </div>
  215. <dl class="py function">
  216. <dt class="sig sig-object py" id="tarfile.open">
  217. <span class="sig-prename descclassname"><span class="pre">tarfile.</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><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">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">fileobj</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">bufsize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10240</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.open" title="Link to this definition">¶</a></dt>
  218. <dd><p>Return a <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> object for the pathname <em>name</em>. For detailed
  219. information on <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> objects and the keyword arguments that are
  220. allowed, see <a class="reference internal" href="#tarfile-objects"><span class="std std-ref">TarFile Objects</span></a>.</p>
  221. <p><em>mode</em> has to be a string of the form <code class="docutils literal notranslate"><span class="pre">'filemode[:compression]'</span></code>, it defaults
  222. to <code class="docutils literal notranslate"><span class="pre">'r'</span></code>. Here is a full list of mode combinations:</p>
  223. <table class="docutils align-default">
  224. <thead>
  225. <tr class="row-odd"><th class="head"><p>mode</p></th>
  226. <th class="head"><p>action</p></th>
  227. </tr>
  228. </thead>
  229. <tbody>
  230. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'r'</span> <span class="pre">or</span> <span class="pre">'r:*'</span></code></p></td>
  231. <td><p>Open for reading with transparent
  232. compression (recommended).</p></td>
  233. </tr>
  234. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'r:'</span></code></p></td>
  235. <td><p>Open for reading exclusively without
  236. compression.</p></td>
  237. </tr>
  238. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'r:gz'</span></code></p></td>
  239. <td><p>Open for reading with gzip compression.</p></td>
  240. </tr>
  241. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'r:bz2'</span></code></p></td>
  242. <td><p>Open for reading with bzip2 compression.</p></td>
  243. </tr>
  244. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'r:xz'</span></code></p></td>
  245. <td><p>Open for reading with lzma compression.</p></td>
  246. </tr>
  247. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'x'</span></code> or
  248. <code class="docutils literal notranslate"><span class="pre">'x:'</span></code></p></td>
  249. <td><p>Create a tarfile exclusively without
  250. compression.
  251. Raise a <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> exception
  252. if it already exists.</p></td>
  253. </tr>
  254. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'x:gz'</span></code></p></td>
  255. <td><p>Create a tarfile with gzip compression.
  256. Raise a <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> exception
  257. if it already exists.</p></td>
  258. </tr>
  259. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'x:bz2'</span></code></p></td>
  260. <td><p>Create a tarfile with bzip2 compression.
  261. Raise a <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> exception
  262. if it already exists.</p></td>
  263. </tr>
  264. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'x:xz'</span></code></p></td>
  265. <td><p>Create a tarfile with lzma compression.
  266. Raise a <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> exception
  267. if it already exists.</p></td>
  268. </tr>
  269. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'a'</span> <span class="pre">or</span> <span class="pre">'a:'</span></code></p></td>
  270. <td><p>Open for appending with no compression. The
  271. file is created if it does not exist.</p></td>
  272. </tr>
  273. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'w'</span> <span class="pre">or</span> <span class="pre">'w:'</span></code></p></td>
  274. <td><p>Open for uncompressed writing.</p></td>
  275. </tr>
  276. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'w:gz'</span></code></p></td>
  277. <td><p>Open for gzip compressed writing.</p></td>
  278. </tr>
  279. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'w:bz2'</span></code></p></td>
  280. <td><p>Open for bzip2 compressed writing.</p></td>
  281. </tr>
  282. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'w:xz'</span></code></p></td>
  283. <td><p>Open for lzma compressed writing.</p></td>
  284. </tr>
  285. </tbody>
  286. </table>
  287. <p>Note that <code class="docutils literal notranslate"><span class="pre">'a:gz'</span></code>, <code class="docutils literal notranslate"><span class="pre">'a:bz2'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a:xz'</span></code> is not possible. If <em>mode</em>
  288. is not suitable to open a certain (compressed) file for reading,
  289. <a class="reference internal" href="#tarfile.ReadError" title="tarfile.ReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ReadError</span></code></a> is raised. Use <em>mode</em> <code class="docutils literal notranslate"><span class="pre">'r'</span></code> to avoid this. If a
  290. compression method is not supported, <a class="reference internal" href="#tarfile.CompressionError" title="tarfile.CompressionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CompressionError</span></code></a> is raised.</p>
  291. <p>If <em>fileobj</em> is specified, it is used as an alternative to a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>
  292. opened in binary mode for <em>name</em>. It is supposed to be at position 0.</p>
  293. <p>For modes <code class="docutils literal notranslate"><span class="pre">'w:gz'</span></code>, <code class="docutils literal notranslate"><span class="pre">'x:gz'</span></code>, <code class="docutils literal notranslate"><span class="pre">'w|gz'</span></code>, <code class="docutils literal notranslate"><span class="pre">'w:bz2'</span></code>, <code class="docutils literal notranslate"><span class="pre">'x:bz2'</span></code>,
  294. <code class="docutils literal notranslate"><span class="pre">'w|bz2'</span></code>, <a class="reference internal" href="#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">tarfile.open()</span></code></a> accepts the keyword argument
  295. <em>compresslevel</em> (default <code class="docutils literal notranslate"><span class="pre">9</span></code>) to specify the compression level of the file.</p>
  296. <p>For modes <code class="docutils literal notranslate"><span class="pre">'w:xz'</span></code> and <code class="docutils literal notranslate"><span class="pre">'x:xz'</span></code>, <a class="reference internal" href="#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">tarfile.open()</span></code></a> accepts the
  297. keyword argument <em>preset</em> to specify the compression level of the file.</p>
  298. <p>For special purposes, there is a second format for <em>mode</em>:
  299. <code class="docutils literal notranslate"><span class="pre">'filemode|[compression]'</span></code>. <a class="reference internal" href="#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">tarfile.open()</span></code></a> will return a <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a>
  300. object that processes its data as a stream of blocks. No random seeking will
  301. be done on the file. If given, <em>fileobj</em> may be any object that has a
  302. <a class="reference internal" href="io.html#io.RawIOBase.read" title="io.RawIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> or <a class="reference internal" href="io.html#io.RawIOBase.write" title="io.RawIOBase.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> method
  303. (depending on the <em>mode</em>) that works with bytes.
  304. <em>bufsize</em> specifies the blocksize and defaults to <code class="docutils literal notranslate"><span class="pre">20</span> <span class="pre">*</span> <span class="pre">512</span></code> bytes.
  305. Use this variant in combination with e.g. <code class="docutils literal notranslate"><span class="pre">sys.stdin.buffer</span></code>, a socket
  306. <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> or a tape device.
  307. However, such a <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> object is limited in that it does
  308. not allow random access, see <a class="reference internal" href="#tar-examples"><span class="std std-ref">Examples</span></a>. The currently
  309. possible modes:</p>
  310. <table class="docutils align-default">
  311. <thead>
  312. <tr class="row-odd"><th class="head"><p>Mode</p></th>
  313. <th class="head"><p>Action</p></th>
  314. </tr>
  315. </thead>
  316. <tbody>
  317. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'r|*'</span></code></p></td>
  318. <td><p>Open a <em>stream</em> of tar blocks for reading
  319. with transparent compression.</p></td>
  320. </tr>
  321. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'r|'</span></code></p></td>
  322. <td><p>Open a <em>stream</em> of uncompressed tar blocks
  323. for reading.</p></td>
  324. </tr>
  325. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'r|gz'</span></code></p></td>
  326. <td><p>Open a gzip compressed <em>stream</em> for
  327. reading.</p></td>
  328. </tr>
  329. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'r|bz2'</span></code></p></td>
  330. <td><p>Open a bzip2 compressed <em>stream</em> for
  331. reading.</p></td>
  332. </tr>
  333. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'r|xz'</span></code></p></td>
  334. <td><p>Open an lzma compressed <em>stream</em> for
  335. reading.</p></td>
  336. </tr>
  337. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'w|'</span></code></p></td>
  338. <td><p>Open an uncompressed <em>stream</em> for writing.</p></td>
  339. </tr>
  340. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'w|gz'</span></code></p></td>
  341. <td><p>Open a gzip compressed <em>stream</em> for
  342. writing.</p></td>
  343. </tr>
  344. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'w|bz2'</span></code></p></td>
  345. <td><p>Open a bzip2 compressed <em>stream</em> for
  346. writing.</p></td>
  347. </tr>
  348. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'w|xz'</span></code></p></td>
  349. <td><p>Open an lzma compressed <em>stream</em> for
  350. writing.</p></td>
  351. </tr>
  352. </tbody>
  353. </table>
  354. <div class="versionchanged">
  355. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <code class="docutils literal notranslate"><span class="pre">'x'</span></code> (exclusive creation) mode was added.</p>
  356. </div>
  357. <div class="versionchanged">
  358. <p><span class="versionmodified changed">Changed in version 3.6: </span>The <em>name</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>
  359. </div>
  360. <div class="versionchanged">
  361. <p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>compresslevel</em> keyword argument also works for streams.</p>
  362. </div>
  363. </dd></dl>
  364. <dl class="py class">
  365. <dt class="sig sig-object py">
  366. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">TarFile</span></span></dt>
  367. <dd><p>Class for reading and writing tar archives. Do not use this class directly:
  368. use <a class="reference internal" href="#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">tarfile.open()</span></code></a> instead. See <a class="reference internal" href="#tarfile-objects"><span class="std std-ref">TarFile Objects</span></a>.</p>
  369. </dd></dl>
  370. <dl class="py function">
  371. <dt class="sig sig-object py" id="tarfile.is_tarfile">
  372. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">is_tarfile</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="#tarfile.is_tarfile" title="Link to this definition">¶</a></dt>
  373. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if <em>name</em> is a tar archive file, that the <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a>
  374. module can read. <em>name</em> may be a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, file, or file-like object.</p>
  375. <div class="versionchanged">
  376. <p><span class="versionmodified changed">Changed in version 3.9: </span>Support for file and file-like objects.</p>
  377. </div>
  378. </dd></dl>
  379. <p>The <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> module defines the following exceptions:</p>
  380. <dl class="py exception">
  381. <dt class="sig sig-object py" id="tarfile.TarError">
  382. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">TarError</span></span><a class="headerlink" href="#tarfile.TarError" title="Link to this definition">¶</a></dt>
  383. <dd><p>Base class for all <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> exceptions.</p>
  384. </dd></dl>
  385. <dl class="py exception">
  386. <dt class="sig sig-object py" id="tarfile.ReadError">
  387. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">ReadError</span></span><a class="headerlink" href="#tarfile.ReadError" title="Link to this definition">¶</a></dt>
  388. <dd><p>Is raised when a tar archive is opened, that either cannot be handled by the
  389. <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> module or is somehow invalid.</p>
  390. </dd></dl>
  391. <dl class="py exception">
  392. <dt class="sig sig-object py" id="tarfile.CompressionError">
  393. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">CompressionError</span></span><a class="headerlink" href="#tarfile.CompressionError" title="Link to this definition">¶</a></dt>
  394. <dd><p>Is raised when a compression method is not supported or when the data cannot be
  395. decoded properly.</p>
  396. </dd></dl>
  397. <dl class="py exception">
  398. <dt class="sig sig-object py" id="tarfile.StreamError">
  399. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">StreamError</span></span><a class="headerlink" href="#tarfile.StreamError" title="Link to this definition">¶</a></dt>
  400. <dd><p>Is raised for the limitations that are typical for stream-like <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a>
  401. objects.</p>
  402. </dd></dl>
  403. <dl class="py exception">
  404. <dt class="sig sig-object py" id="tarfile.ExtractError">
  405. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">ExtractError</span></span><a class="headerlink" href="#tarfile.ExtractError" title="Link to this definition">¶</a></dt>
  406. <dd><p>Is raised for <em>non-fatal</em> errors when using <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.extract()</span></code></a>, but only if
  407. <a class="reference internal" href="#tarfile.TarFile.errorlevel" title="tarfile.TarFile.errorlevel"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TarFile.errorlevel</span></code></a><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">2</span></code>.</p>
  408. </dd></dl>
  409. <dl class="py exception">
  410. <dt class="sig sig-object py" id="tarfile.HeaderError">
  411. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">HeaderError</span></span><a class="headerlink" href="#tarfile.HeaderError" title="Link to this definition">¶</a></dt>
  412. <dd><p>Is raised by <a class="reference internal" href="#tarfile.TarInfo.frombuf" title="tarfile.TarInfo.frombuf"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarInfo.frombuf()</span></code></a> if the buffer it gets is invalid.</p>
  413. </dd></dl>
  414. <dl class="py exception">
  415. <dt class="sig sig-object py" id="tarfile.FilterError">
  416. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">FilterError</span></span><a class="headerlink" href="#tarfile.FilterError" title="Link to this definition">¶</a></dt>
  417. <dd><p>Base class for members <a class="reference internal" href="#tarfile-extraction-refuse"><span class="std std-ref">refused</span></a> by
  418. filters.</p>
  419. <dl class="py attribute">
  420. <dt class="sig sig-object py" id="tarfile.FilterError.tarinfo">
  421. <span class="sig-name descname"><span class="pre">tarinfo</span></span><a class="headerlink" href="#tarfile.FilterError.tarinfo" title="Link to this definition">¶</a></dt>
  422. <dd><p>Information about the member that the filter refused to extract,
  423. as <a class="reference internal" href="#tarinfo-objects"><span class="std std-ref">TarInfo</span></a>.</p>
  424. </dd></dl>
  425. </dd></dl>
  426. <dl class="py exception">
  427. <dt class="sig sig-object py" id="tarfile.AbsolutePathError">
  428. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">AbsolutePathError</span></span><a class="headerlink" href="#tarfile.AbsolutePathError" title="Link to this definition">¶</a></dt>
  429. <dd><p>Raised to refuse extracting a member with an absolute path.</p>
  430. </dd></dl>
  431. <dl class="py exception">
  432. <dt class="sig sig-object py" id="tarfile.OutsideDestinationError">
  433. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">OutsideDestinationError</span></span><a class="headerlink" href="#tarfile.OutsideDestinationError" title="Link to this definition">¶</a></dt>
  434. <dd><p>Raised to refuse extracting a member outside the destination directory.</p>
  435. </dd></dl>
  436. <dl class="py exception">
  437. <dt class="sig sig-object py" id="tarfile.SpecialFileError">
  438. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">SpecialFileError</span></span><a class="headerlink" href="#tarfile.SpecialFileError" title="Link to this definition">¶</a></dt>
  439. <dd><p>Raised to refuse extracting a special file (e.g. a device or pipe).</p>
  440. </dd></dl>
  441. <dl class="py exception">
  442. <dt class="sig sig-object py" id="tarfile.AbsoluteLinkError">
  443. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">AbsoluteLinkError</span></span><a class="headerlink" href="#tarfile.AbsoluteLinkError" title="Link to this definition">¶</a></dt>
  444. <dd><p>Raised to refuse extracting a symbolic link with an absolute path.</p>
  445. </dd></dl>
  446. <dl class="py exception">
  447. <dt class="sig sig-object py" id="tarfile.LinkOutsideDestinationError">
  448. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">LinkOutsideDestinationError</span></span><a class="headerlink" href="#tarfile.LinkOutsideDestinationError" title="Link to this definition">¶</a></dt>
  449. <dd><p>Raised to refuse extracting a symbolic link pointing outside the destination
  450. directory.</p>
  451. </dd></dl>
  452. <p>The following constants are available at the module level:</p>
  453. <dl class="py data">
  454. <dt class="sig sig-object py" id="tarfile.ENCODING">
  455. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">ENCODING</span></span><a class="headerlink" href="#tarfile.ENCODING" title="Link to this definition">¶</a></dt>
  456. <dd><p>The default character encoding: <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code> on Windows, the value returned by
  457. <a class="reference internal" href="sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a> otherwise.</p>
  458. </dd></dl>
  459. <dl class="py data">
  460. <dt class="sig sig-object py" id="tarfile.REGTYPE">
  461. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">REGTYPE</span></span><a class="headerlink" href="#tarfile.REGTYPE" title="Link to this definition">¶</a></dt>
  462. <dt class="sig sig-object py" id="tarfile.AREGTYPE">
  463. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">AREGTYPE</span></span><a class="headerlink" href="#tarfile.AREGTYPE" title="Link to this definition">¶</a></dt>
  464. <dd><p>A regular file <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  465. </dd></dl>
  466. <dl class="py data">
  467. <dt class="sig sig-object py" id="tarfile.LNKTYPE">
  468. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">LNKTYPE</span></span><a class="headerlink" href="#tarfile.LNKTYPE" title="Link to this definition">¶</a></dt>
  469. <dd><p>A link (inside tarfile) <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  470. </dd></dl>
  471. <dl class="py data">
  472. <dt class="sig sig-object py" id="tarfile.SYMTYPE">
  473. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">SYMTYPE</span></span><a class="headerlink" href="#tarfile.SYMTYPE" title="Link to this definition">¶</a></dt>
  474. <dd><p>A symbolic link <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  475. </dd></dl>
  476. <dl class="py data">
  477. <dt class="sig sig-object py" id="tarfile.CHRTYPE">
  478. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">CHRTYPE</span></span><a class="headerlink" href="#tarfile.CHRTYPE" title="Link to this definition">¶</a></dt>
  479. <dd><p>A character special device <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  480. </dd></dl>
  481. <dl class="py data">
  482. <dt class="sig sig-object py" id="tarfile.BLKTYPE">
  483. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">BLKTYPE</span></span><a class="headerlink" href="#tarfile.BLKTYPE" title="Link to this definition">¶</a></dt>
  484. <dd><p>A block special device <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  485. </dd></dl>
  486. <dl class="py data">
  487. <dt class="sig sig-object py" id="tarfile.DIRTYPE">
  488. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">DIRTYPE</span></span><a class="headerlink" href="#tarfile.DIRTYPE" title="Link to this definition">¶</a></dt>
  489. <dd><p>A directory <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  490. </dd></dl>
  491. <dl class="py data">
  492. <dt class="sig sig-object py" id="tarfile.FIFOTYPE">
  493. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">FIFOTYPE</span></span><a class="headerlink" href="#tarfile.FIFOTYPE" title="Link to this definition">¶</a></dt>
  494. <dd><p>A FIFO special device <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  495. </dd></dl>
  496. <dl class="py data">
  497. <dt class="sig sig-object py" id="tarfile.CONTTYPE">
  498. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">CONTTYPE</span></span><a class="headerlink" href="#tarfile.CONTTYPE" title="Link to this definition">¶</a></dt>
  499. <dd><p>A contiguous file <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  500. </dd></dl>
  501. <dl class="py data">
  502. <dt class="sig sig-object py" id="tarfile.GNUTYPE_LONGNAME">
  503. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">GNUTYPE_LONGNAME</span></span><a class="headerlink" href="#tarfile.GNUTYPE_LONGNAME" title="Link to this definition">¶</a></dt>
  504. <dd><p>A GNU tar longname <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  505. </dd></dl>
  506. <dl class="py data">
  507. <dt class="sig sig-object py" id="tarfile.GNUTYPE_LONGLINK">
  508. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">GNUTYPE_LONGLINK</span></span><a class="headerlink" href="#tarfile.GNUTYPE_LONGLINK" title="Link to this definition">¶</a></dt>
  509. <dd><p>A GNU tar longlink <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  510. </dd></dl>
  511. <dl class="py data">
  512. <dt class="sig sig-object py" id="tarfile.GNUTYPE_SPARSE">
  513. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">GNUTYPE_SPARSE</span></span><a class="headerlink" href="#tarfile.GNUTYPE_SPARSE" title="Link to this definition">¶</a></dt>
  514. <dd><p>A GNU tar sparse file <a class="reference internal" href="#tarfile.TarInfo.type" title="tarfile.TarInfo.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>.</p>
  515. </dd></dl>
  516. <p>Each of the following constants defines a tar archive format that the
  517. <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> module is able to create. See section <a class="reference internal" href="#tar-formats"><span class="std std-ref">Supported tar formats</span></a> for
  518. details.</p>
  519. <dl class="py data">
  520. <dt class="sig sig-object py" id="tarfile.USTAR_FORMAT">
  521. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">USTAR_FORMAT</span></span><a class="headerlink" href="#tarfile.USTAR_FORMAT" title="Link to this definition">¶</a></dt>
  522. <dd><p>POSIX.1-1988 (ustar) format.</p>
  523. </dd></dl>
  524. <dl class="py data">
  525. <dt class="sig sig-object py" id="tarfile.GNU_FORMAT">
  526. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">GNU_FORMAT</span></span><a class="headerlink" href="#tarfile.GNU_FORMAT" title="Link to this definition">¶</a></dt>
  527. <dd><p>GNU tar format.</p>
  528. </dd></dl>
  529. <dl class="py data">
  530. <dt class="sig sig-object py" id="tarfile.PAX_FORMAT">
  531. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">PAX_FORMAT</span></span><a class="headerlink" href="#tarfile.PAX_FORMAT" title="Link to this definition">¶</a></dt>
  532. <dd><p>POSIX.1-2001 (pax) format.</p>
  533. </dd></dl>
  534. <dl class="py data">
  535. <dt class="sig sig-object py" id="tarfile.DEFAULT_FORMAT">
  536. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">DEFAULT_FORMAT</span></span><a class="headerlink" href="#tarfile.DEFAULT_FORMAT" title="Link to this definition">¶</a></dt>
  537. <dd><p>The default format for creating archives. This is currently <a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PAX_FORMAT</span></code></a>.</p>
  538. <div class="versionchanged">
  539. <p><span class="versionmodified changed">Changed in version 3.8: </span>The default format for new archives was changed to
  540. <a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PAX_FORMAT</span></code></a> from <a class="reference internal" href="#tarfile.GNU_FORMAT" title="tarfile.GNU_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">GNU_FORMAT</span></code></a>.</p>
  541. </div>
  542. </dd></dl>
  543. <div class="admonition seealso">
  544. <p class="admonition-title">See also</p>
  545. <dl class="simple">
  546. <dt>Module <a class="reference internal" href="zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a></dt><dd><p>Documentation of the <a class="reference internal" href="zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> standard module.</p>
  547. </dd>
  548. <dt><a class="reference internal" href="shutil.html#archiving-operations"><span class="std std-ref">Archiving operations</span></a></dt><dd><p>Documentation of the higher-level archiving facilities provided by the
  549. standard <a class="reference internal" href="shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a> module.</p>
  550. </dd>
  551. <dt><a class="reference external" href="https://www.gnu.org/software/tar/manual/html_node/Standard.html">GNU tar manual, Basic Tar Format</a></dt><dd><p>Documentation for tar archive files, including GNU tar extensions.</p>
  552. </dd>
  553. </dl>
  554. </div>
  555. <section id="tarfile-objects">
  556. <span id="id1"></span><h2>TarFile Objects<a class="headerlink" href="#tarfile-objects" title="Link to this heading">¶</a></h2>
  557. <p>The <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> object provides an interface to a tar archive. A tar
  558. archive is a sequence of blocks. An archive member (a stored file) is made up of
  559. a header block followed by data blocks. It is possible to store a file in a tar
  560. archive several times. Each archive member is represented by a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a>
  561. object, see <a class="reference internal" href="#tarinfo-objects"><span class="std std-ref">TarInfo Objects</span></a> for details.</p>
  562. <p>A <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> object can be used as a context manager in a <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>
  563. statement. It will automatically be closed when the block is completed. Please
  564. note that in the event of an exception an archive opened for writing will not
  565. be finalized; only the internally used file object will be closed. See the
  566. <a class="reference internal" href="#tar-examples"><span class="std std-ref">Examples</span></a> section for a use case.</p>
  567. <div class="versionadded">
  568. <p><span class="versionmodified added">New in version 3.2: </span>Added support for the context management protocol.</p>
  569. </div>
  570. <dl class="py class">
  571. <dt class="sig sig-object py" id="tarfile.TarFile">
  572. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">TarFile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</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">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">fileobj</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">format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DEFAULT_FORMAT</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tarinfo</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">TarInfo</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dereference</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignore_zeros</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">ENCODING</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'surrogateescape'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pax_headers</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">debug</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errorlevel</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="#tarfile.TarFile" title="Link to this definition">¶</a></dt>
  573. <dd><p>All following arguments are optional and can be accessed as instance attributes
  574. as well.</p>
  575. <p><em>name</em> is the pathname of the archive. <em>name</em> may be a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.
  576. It can be omitted if <em>fileobj</em> is given.
  577. In this case, the file object’s <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> attribute is used if it exists.</p>
  578. <p><em>mode</em> is either <code class="docutils literal notranslate"><span class="pre">'r'</span></code> to read from an existing archive, <code class="docutils literal notranslate"><span class="pre">'a'</span></code> to append
  579. data to an existing file, <code class="docutils literal notranslate"><span class="pre">'w'</span></code> to create a new file overwriting an existing
  580. one, or <code class="docutils literal notranslate"><span class="pre">'x'</span></code> to create a new file only if it does not already exist.</p>
  581. <p>If <em>fileobj</em> is given, it is used for reading or writing data. If it can be
  582. determined, <em>mode</em> is overridden by <em>fileobj</em>’s mode. <em>fileobj</em> will be used
  583. from position 0.</p>
  584. <div class="admonition note">
  585. <p class="admonition-title">Note</p>
  586. <p><em>fileobj</em> is not closed, when <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> is closed.</p>
  587. </div>
  588. <p><em>format</em> controls the archive format for writing. It must be one of the constants
  589. <a class="reference internal" href="#tarfile.USTAR_FORMAT" title="tarfile.USTAR_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">USTAR_FORMAT</span></code></a>, <a class="reference internal" href="#tarfile.GNU_FORMAT" title="tarfile.GNU_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">GNU_FORMAT</span></code></a> or <a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PAX_FORMAT</span></code></a> that are
  590. defined at module level. When reading, format will be automatically detected, even
  591. if different formats are present in a single archive.</p>
  592. <p>The <em>tarinfo</em> argument can be used to replace the default <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> class
  593. with a different one.</p>
  594. <p>If <em>dereference</em> is <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>, add symbolic and hard links to the archive. If it
  595. is <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, add the content of the target files to the archive. This has no
  596. effect on systems that do not support symbolic links.</p>
  597. <p>If <em>ignore_zeros</em> is <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>, treat an empty block as the end of the archive.
  598. If it is <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, skip empty (and invalid) blocks and try to get as many members
  599. as possible. This is only useful for reading concatenated or damaged archives.</p>
  600. <p><em>debug</em> can be set from <code class="docutils literal notranslate"><span class="pre">0</span></code> (no debug messages) up to <code class="docutils literal notranslate"><span class="pre">3</span></code> (all debug
  601. messages). The messages are written to <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>.</p>
  602. <p><em>errorlevel</em> controls how extraction errors are handled,
  603. see <a class="reference internal" href="#tarfile.TarFile.errorlevel" title="tarfile.TarFile.errorlevel"><code class="xref py py-attr docutils literal notranslate"><span class="pre">the</span> <span class="pre">corresponding</span> <span class="pre">attribute</span></code></a>.</p>
  604. <p>The <em>encoding</em> and <em>errors</em> arguments define the character encoding to be
  605. used for reading or writing the archive and how conversion errors are going
  606. to be handled. The default settings will work for most users.
  607. See section <a class="reference internal" href="#tar-unicode"><span class="std std-ref">Unicode issues</span></a> for in-depth information.</p>
  608. <p>The <em>pax_headers</em> argument is an optional dictionary of strings which
  609. will be added as a pax global header if <em>format</em> is <a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PAX_FORMAT</span></code></a>.</p>
  610. <div class="versionchanged">
  611. <p><span class="versionmodified changed">Changed in version 3.2: </span>Use <code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> as the default for the <em>errors</em> argument.</p>
  612. </div>
  613. <div class="versionchanged">
  614. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <code class="docutils literal notranslate"><span class="pre">'x'</span></code> (exclusive creation) mode was added.</p>
  615. </div>
  616. <div class="versionchanged">
  617. <p><span class="versionmodified changed">Changed in version 3.6: </span>The <em>name</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>
  618. </div>
  619. </dd></dl>
  620. <dl class="py method">
  621. <dt class="sig sig-object py" id="tarfile.TarFile.open">
  622. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">TarFile.</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">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarFile.open" title="Link to this definition">¶</a></dt>
  623. <dd><p>Alternative constructor. The <a class="reference internal" href="#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">tarfile.open()</span></code></a> function is actually a
  624. shortcut to this classmethod.</p>
  625. </dd></dl>
  626. <dl class="py method">
  627. <dt class="sig sig-object py" id="tarfile.TarFile.getmember">
  628. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">getmember</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="#tarfile.TarFile.getmember" title="Link to this definition">¶</a></dt>
  629. <dd><p>Return a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object for member <em>name</em>. If <em>name</em> can not be found
  630. in the archive, <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> is raised.</p>
  631. <div class="admonition note">
  632. <p class="admonition-title">Note</p>
  633. <p>If a member occurs more than once in the archive, its last occurrence is assumed
  634. to be the most up-to-date version.</p>
  635. </div>
  636. </dd></dl>
  637. <dl class="py method">
  638. <dt class="sig sig-object py" id="tarfile.TarFile.getmembers">
  639. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">getmembers</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarFile.getmembers" title="Link to this definition">¶</a></dt>
  640. <dd><p>Return the members of the archive as a list of <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> objects. The
  641. list has the same order as the members in the archive.</p>
  642. </dd></dl>
  643. <dl class="py method">
  644. <dt class="sig sig-object py" id="tarfile.TarFile.getnames">
  645. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">getnames</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarFile.getnames" title="Link to this definition">¶</a></dt>
  646. <dd><p>Return the members as a list of their names. It has the same order as the list
  647. returned by <a class="reference internal" href="#tarfile.TarFile.getmembers" title="tarfile.TarFile.getmembers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getmembers()</span></code></a>.</p>
  648. </dd></dl>
  649. <dl class="py method">
  650. <dt class="sig sig-object py" id="tarfile.TarFile.list">
  651. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">list</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">verbose</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="o"><span class="pre">*</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><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarFile.list" title="Link to this definition">¶</a></dt>
  652. <dd><p>Print a table of contents to <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>. If <em>verbose</em> is <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>,
  653. only the names of the members are printed. If it is <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, output
  654. similar to that of <strong class="program">ls -l</strong> is produced. If optional <em>members</em> is
  655. given, it must be a subset of the list returned by <a class="reference internal" href="#tarfile.TarFile.getmembers" title="tarfile.TarFile.getmembers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getmembers()</span></code></a>.</p>
  656. <div class="versionchanged">
  657. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>members</em> parameter.</p>
  658. </div>
  659. </dd></dl>
  660. <dl class="py method">
  661. <dt class="sig sig-object py" id="tarfile.TarFile.next">
  662. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">next</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarFile.next" title="Link to this definition">¶</a></dt>
  663. <dd><p>Return the next member of the archive as a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object, when
  664. <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> is opened for reading. Return <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> if there is no more
  665. available.</p>
  666. </dd></dl>
  667. <dl class="py method">
  668. <dt class="sig sig-object py" id="tarfile.TarFile.extractall">
  669. <span class="sig-prename descclassname"><span class="pre">TarFile.</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">'.'</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">numeric_owner</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filter</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="#tarfile.TarFile.extractall" title="Link to this definition">¶</a></dt>
  670. <dd><p>Extract all members from the archive to the current working directory or
  671. directory <em>path</em>. If optional <em>members</em> is given, it must be a subset of the
  672. list returned by <a class="reference internal" href="#tarfile.TarFile.getmembers" title="tarfile.TarFile.getmembers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getmembers()</span></code></a>. Directory information like owner,
  673. modification time and permissions are set after all members have been extracted.
  674. This is done to work around two problems: A directory’s modification time is
  675. reset each time a file is created in it. And, if a directory’s permissions do
  676. not allow writing, extracting files to it will fail.</p>
  677. <p>If <em>numeric_owner</em> is <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, the uid and gid numbers from the tarfile
  678. are used to set the owner/group for the extracted files. Otherwise, the named
  679. values from the tarfile are used.</p>
  680. <p>The <em>filter</em> argument specifies how <code class="docutils literal notranslate"><span class="pre">members</span></code> are modified or rejected
  681. before extraction.
  682. See <a class="reference internal" href="#tarfile-extraction-filter"><span class="std std-ref">Extraction filters</span></a> for details.
  683. It is recommended to set this explicitly depending on which <em>tar</em> features
  684. you need to support.</p>
  685. <div class="admonition warning">
  686. <p class="admonition-title">Warning</p>
  687. <p>Never extract archives from untrusted sources without prior inspection.
  688. It is possible that files are created outside of <em>path</em>, e.g. members
  689. that have absolute filenames starting with <code class="docutils literal notranslate"><span class="pre">&quot;/&quot;</span></code> or filenames with two
  690. dots <code class="docutils literal notranslate"><span class="pre">&quot;..&quot;</span></code>.</p>
  691. <p>Set <code class="docutils literal notranslate"><span class="pre">filter='data'</span></code> to prevent the most dangerous security issues,
  692. and read the <a class="reference internal" href="#tarfile-extraction-filter"><span class="std std-ref">Extraction filters</span></a> section for details.</p>
  693. </div>
  694. <div class="versionchanged">
  695. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>numeric_owner</em> parameter.</p>
  696. </div>
  697. <div class="versionchanged">
  698. <p><span class="versionmodified changed">Changed in version 3.6: </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>
  699. </div>
  700. <div class="versionchanged">
  701. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>filter</em> parameter.</p>
  702. </div>
  703. </dd></dl>
  704. <dl class="py method">
  705. <dt class="sig sig-object py" id="tarfile.TarFile.extract">
  706. <span class="sig-prename descclassname"><span class="pre">TarFile.</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">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">set_attrs</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">numeric_owner</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filter</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="#tarfile.TarFile.extract" title="Link to this definition">¶</a></dt>
  707. <dd><p>Extract a member from the archive to the current working directory, using its
  708. full name. Its file information is extracted as accurately as possible. <em>member</em>
  709. may be a filename or a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object. You can specify a different
  710. directory using <em>path</em>. <em>path</em> may be a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.
  711. File attributes (owner, mtime, mode) are set unless <em>set_attrs</em> is false.</p>
  712. <p>The <em>numeric_owner</em> and <em>filter</em> arguments are the same as
  713. for <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>.</p>
  714. <div class="admonition note">
  715. <p class="admonition-title">Note</p>
  716. <p>The <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> method does not take care of several extraction issues.
  717. In most cases you should consider using the <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a> method.</p>
  718. </div>
  719. <div class="admonition warning">
  720. <p class="admonition-title">Warning</p>
  721. <p>See the warning for <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>.</p>
  722. <p>Set <code class="docutils literal notranslate"><span class="pre">filter='data'</span></code> to prevent the most dangerous security issues,
  723. and read the <a class="reference internal" href="#tarfile-extraction-filter"><span class="std std-ref">Extraction filters</span></a> section for details.</p>
  724. </div>
  725. <div class="versionchanged">
  726. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>set_attrs</em> parameter.</p>
  727. </div>
  728. <div class="versionchanged">
  729. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>numeric_owner</em> parameter.</p>
  730. </div>
  731. <div class="versionchanged">
  732. <p><span class="versionmodified changed">Changed in version 3.6: </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>
  733. </div>
  734. <div class="versionchanged">
  735. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>filter</em> parameter.</p>
  736. </div>
  737. </dd></dl>
  738. <dl class="py method">
  739. <dt class="sig sig-object py" id="tarfile.TarFile.extractfile">
  740. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">extractfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">member</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarFile.extractfile" title="Link to this definition">¶</a></dt>
  741. <dd><p>Extract a member from the archive as a file object. <em>member</em> may be
  742. a filename or a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object. If <em>member</em> is a regular file or
  743. a link, an <a class="reference internal" href="io.html#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedReader</span></code></a> object is returned. For all other
  744. existing members, <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> is returned. If <em>member</em> does not appear
  745. in the archive, <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> is raised.</p>
  746. <div class="versionchanged">
  747. <p><span class="versionmodified changed">Changed in version 3.3: </span>Return an <a class="reference internal" href="io.html#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedReader</span></code></a> object.</p>
  748. </div>
  749. </dd></dl>
  750. <dl class="py attribute">
  751. <dt class="sig sig-object py" id="tarfile.TarFile.errorlevel">
  752. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">errorlevel</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarFile.errorlevel" title="Link to this definition">¶</a></dt>
  753. <dd><p>If <em>errorlevel</em> is <code class="docutils literal notranslate"><span class="pre">0</span></code>, errors are ignored when using <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.extract()</span></code></a>
  754. and <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.extractall()</span></code></a>.
  755. Nevertheless, they appear as error messages in the debug output when
  756. <em>debug</em> is greater than 0.
  757. If <code class="docutils literal notranslate"><span class="pre">1</span></code> (the default), all <em>fatal</em> errors are raised as <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> or
  758. <a class="reference internal" href="#tarfile.FilterError" title="tarfile.FilterError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FilterError</span></code></a> exceptions. If <code class="docutils literal notranslate"><span class="pre">2</span></code>, all <em>non-fatal</em> errors are raised
  759. as <a class="reference internal" href="#tarfile.TarError" title="tarfile.TarError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TarError</span></code></a> exceptions as well.</p>
  760. <p>Some exceptions, e.g. ones caused by wrong argument types or data
  761. corruption, are always raised.</p>
  762. <p>Custom <a class="reference internal" href="#tarfile-extraction-filter"><span class="std std-ref">extraction filters</span></a>
  763. should raise <a class="reference internal" href="#tarfile.FilterError" title="tarfile.FilterError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FilterError</span></code></a> for <em>fatal</em> errors
  764. and <a class="reference internal" href="#tarfile.ExtractError" title="tarfile.ExtractError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExtractError</span></code></a> for <em>non-fatal</em> ones.</p>
  765. <p>Note that when an exception is raised, the archive may be partially
  766. extracted. It is the user’s responsibility to clean up.</p>
  767. </dd></dl>
  768. <dl class="py attribute">
  769. <dt class="sig sig-object py" id="tarfile.TarFile.extraction_filter">
  770. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">extraction_filter</span></span><a class="headerlink" href="#tarfile.TarFile.extraction_filter" title="Link to this definition">¶</a></dt>
  771. <dd><div class="versionadded">
  772. <p><span class="versionmodified added">New in version 3.12.</span></p>
  773. </div>
  774. <p>The <a class="reference internal" href="#tarfile-extraction-filter"><span class="std std-ref">extraction filter</span></a> used
  775. as a default for the <em>filter</em> argument of <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a>
  776. and <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>.</p>
  777. <p>The attribute may be <code class="docutils literal notranslate"><span class="pre">None</span></code> or a callable.
  778. String names are not allowed for this attribute, unlike the <em>filter</em>
  779. argument to <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a>.</p>
  780. <p>If <code class="docutils literal notranslate"><span class="pre">extraction_filter</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default),
  781. calling an extraction method without a <em>filter</em> argument will raise a
  782. <code class="docutils literal notranslate"><span class="pre">DeprecationWarning</span></code>,
  783. and fall back to the <a class="reference internal" href="#tarfile.fully_trusted_filter" title="tarfile.fully_trusted_filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">fully_trusted</span></code></a> filter,
  784. whose dangerous behavior matches previous versions of Python.</p>
  785. <p>In Python 3.14+, leaving <code class="docutils literal notranslate"><span class="pre">extraction_filter=None</span></code> will cause
  786. extraction methods to use the <a class="reference internal" href="#tarfile.data_filter" title="tarfile.data_filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">data</span></code></a> filter by default.</p>
  787. <p>The attribute may be set on instances or overridden in subclasses.
  788. It also is possible to set it on the <code class="docutils literal notranslate"><span class="pre">TarFile</span></code> class itself to set a
  789. global default, although, since it affects all uses of <em>tarfile</em>,
  790. it is best practice to only do so in top-level applications or
  791. <a class="reference internal" href="site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span> <span class="pre">configuration</span></code></a>.
  792. To set a global default this way, a filter function needs to be wrapped in
  793. <a class="reference internal" href="functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a> to prevent injection of a <code class="docutils literal notranslate"><span class="pre">self</span></code> argument.</p>
  794. </dd></dl>
  795. <dl class="py method">
  796. <dt class="sig sig-object py" id="tarfile.TarFile.add">
  797. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">add</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">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">recursive</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filter</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="#tarfile.TarFile.add" title="Link to this definition">¶</a></dt>
  798. <dd><p>Add the file <em>name</em> to the archive. <em>name</em> may be any type of file
  799. (directory, fifo, symbolic link, etc.). If given, <em>arcname</em> specifies an
  800. alternative name for the file in the archive. Directories are added
  801. recursively by default. This can be avoided by setting <em>recursive</em> to
  802. <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>. Recursion adds entries in sorted order.
  803. If <em>filter</em> is given, it
  804. should be a function that takes a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object argument and
  805. returns the changed <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object. If it instead returns
  806. <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> the <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object will be excluded from the
  807. archive. See <a class="reference internal" href="#tar-examples"><span class="std std-ref">Examples</span></a> for an example.</p>
  808. <div class="versionchanged">
  809. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>filter</em> parameter.</p>
  810. </div>
  811. <div class="versionchanged">
  812. <p><span class="versionmodified changed">Changed in version 3.7: </span>Recursion adds entries in sorted order.</p>
  813. </div>
  814. </dd></dl>
  815. <dl class="py method">
  816. <dt class="sig sig-object py" id="tarfile.TarFile.addfile">
  817. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">addfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tarinfo</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fileobj</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="#tarfile.TarFile.addfile" title="Link to this definition">¶</a></dt>
  818. <dd><p>Add the <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object <em>tarinfo</em> to the archive. If <em>fileobj</em> is given,
  819. it should be a <a class="reference internal" href="../glossary.html#term-binary-file"><span class="xref std std-term">binary file</span></a>, and
  820. <code class="docutils literal notranslate"><span class="pre">tarinfo.size</span></code> bytes are read from it and added to the archive. You can
  821. create <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> objects directly, or by using <a class="reference internal" href="#tarfile.TarFile.gettarinfo" title="tarfile.TarFile.gettarinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">gettarinfo()</span></code></a>.</p>
  822. </dd></dl>
  823. <dl class="py method">
  824. <dt class="sig sig-object py" id="tarfile.TarFile.gettarinfo">
  825. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">gettarinfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</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">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">fileobj</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="#tarfile.TarFile.gettarinfo" title="Link to this definition">¶</a></dt>
  826. <dd><p>Create a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object from the result of <a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> or
  827. equivalent on an existing file. The file is either named by <em>name</em>, or
  828. specified as a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>fileobj</em> with a file descriptor.
  829. <em>name</em> may be a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>. If
  830. given, <em>arcname</em> specifies an alternative name for the file in the
  831. archive, otherwise, the name is taken from <em>fileobj</em>’s
  832. <a class="reference internal" href="io.html#io.FileIO.name" title="io.FileIO.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> attribute, or the <em>name</em> argument. The name
  833. should be a text string.</p>
  834. <p>You can modify
  835. some of the <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a>’s attributes before you add it using <a class="reference internal" href="#tarfile.TarFile.addfile" title="tarfile.TarFile.addfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addfile()</span></code></a>.
  836. If the file object is not an ordinary file object positioned at the
  837. beginning of the file, attributes such as <a class="reference internal" href="#tarfile.TarInfo.size" title="tarfile.TarInfo.size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">size</span></code></a> may need
  838. modifying. This is the case for objects such as <a class="reference internal" href="gzip.html#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">GzipFile</span></code></a>.
  839. The <a class="reference internal" href="#tarfile.TarInfo.name" title="tarfile.TarInfo.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> may also be modified, in which case <em>arcname</em>
  840. could be a dummy string.</p>
  841. <div class="versionchanged">
  842. <p><span class="versionmodified changed">Changed in version 3.6: </span>The <em>name</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>
  843. </div>
  844. </dd></dl>
  845. <dl class="py method">
  846. <dt class="sig sig-object py" id="tarfile.TarFile.close">
  847. <span class="sig-prename descclassname"><span class="pre">TarFile.</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="#tarfile.TarFile.close" title="Link to this definition">¶</a></dt>
  848. <dd><p>Close the <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a>. In write mode, two finishing zero blocks are
  849. appended to the archive.</p>
  850. </dd></dl>
  851. <dl class="py attribute">
  852. <dt class="sig sig-object py" id="tarfile.TarFile.pax_headers">
  853. <span class="sig-prename descclassname"><span class="pre">TarFile.</span></span><span class="sig-name descname"><span class="pre">pax_headers</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#dict" title="dict"><span class="pre">dict</span></a></em><a class="headerlink" href="#tarfile.TarFile.pax_headers" title="Link to this definition">¶</a></dt>
  854. <dd><p>A dictionary containing key-value pairs of pax global headers.</p>
  855. </dd></dl>
  856. </section>
  857. <section id="tarinfo-objects">
  858. <span id="id2"></span><h2>TarInfo Objects<a class="headerlink" href="#tarinfo-objects" title="Link to this heading">¶</a></h2>
  859. <p>A <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object represents one member in a <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a>. Aside
  860. from storing all required attributes of a file (like file type, size, time,
  861. permissions, owner etc.), it provides some useful methods to determine its type.
  862. It does <em>not</em> contain the file’s data itself.</p>
  863. <p><a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> objects are returned by <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a>’s methods
  864. <a class="reference internal" href="#tarfile.TarFile.getmember" title="tarfile.TarFile.getmember"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getmember()</span></code></a>, <a class="reference internal" href="#tarfile.TarFile.getmembers" title="tarfile.TarFile.getmembers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getmembers()</span></code></a> and
  865. <a class="reference internal" href="#tarfile.TarFile.gettarinfo" title="tarfile.TarFile.gettarinfo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">gettarinfo()</span></code></a>.</p>
  866. <p>Modifying the objects returned by <a class="reference internal" href="#tarfile.TarFile.getmember" title="tarfile.TarFile.getmember"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getmember()</span></code></a> or
  867. <a class="reference internal" href="#tarfile.TarFile.getmembers" title="tarfile.TarFile.getmembers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getmembers()</span></code></a> will affect all subsequent
  868. operations on the archive.
  869. For cases where this is unwanted, you can use <a class="reference internal" href="copy.html#module-copy" title="copy: Shallow and deep copy operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy.copy()</span></code></a> or
  870. call the <a class="reference internal" href="#tarfile.TarInfo.replace" title="tarfile.TarInfo.replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">replace()</span></code></a> method to create a modified copy in one step.</p>
  871. <p>Several attributes can be set to <code class="docutils literal notranslate"><span class="pre">None</span></code> to indicate that a piece of metadata
  872. is unused or unknown.
  873. Different <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> methods handle <code class="docutils literal notranslate"><span class="pre">None</span></code> differently:</p>
  874. <ul class="simple">
  875. <li><p>The <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> or <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a> methods will
  876. ignore the corresponding metadata, leaving it set to a default.</p></li>
  877. <li><p><a class="reference internal" href="#tarfile.TarFile.addfile" title="tarfile.TarFile.addfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addfile()</span></code></a> will fail.</p></li>
  878. <li><p><a class="reference internal" href="#tarfile.TarFile.list" title="tarfile.TarFile.list"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list()</span></code></a> will print a placeholder string.</p></li>
  879. </ul>
  880. <dl class="py class">
  881. <dt class="sig sig-object py" id="tarfile.TarInfo">
  882. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">TarInfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</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="#tarfile.TarInfo" title="Link to this definition">¶</a></dt>
  883. <dd><p>Create a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object.</p>
  884. </dd></dl>
  885. <dl class="py method">
  886. <dt class="sig sig-object py" id="tarfile.TarInfo.frombuf">
  887. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">frombuf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">buf</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.frombuf" title="Link to this definition">¶</a></dt>
  888. <dd><p>Create and return a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object from string buffer <em>buf</em>.</p>
  889. <p>Raises <a class="reference internal" href="#tarfile.HeaderError" title="tarfile.HeaderError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HeaderError</span></code></a> if the buffer is invalid.</p>
  890. </dd></dl>
  891. <dl class="py method">
  892. <dt class="sig sig-object py" id="tarfile.TarInfo.fromtarfile">
  893. <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">fromtarfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tarfile</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.fromtarfile" title="Link to this definition">¶</a></dt>
  894. <dd><p>Read the next member from the <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> object <em>tarfile</em> and return it as
  895. a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object.</p>
  896. </dd></dl>
  897. <dl class="py method">
  898. <dt class="sig sig-object py" id="tarfile.TarInfo.tobuf">
  899. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">tobuf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DEFAULT_FORMAT</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">ENCODING</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'surrogateescape'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.tobuf" title="Link to this definition">¶</a></dt>
  900. <dd><p>Create a string buffer from a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object. For information on the
  901. arguments see the constructor of the <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> class.</p>
  902. <div class="versionchanged">
  903. <p><span class="versionmodified changed">Changed in version 3.2: </span>Use <code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> as the default for the <em>errors</em> argument.</p>
  904. </div>
  905. </dd></dl>
  906. <p>A <code class="docutils literal notranslate"><span class="pre">TarInfo</span></code> object has the following public data attributes:</p>
  907. <dl class="py attribute">
  908. <dt class="sig sig-object py" id="tarfile.TarInfo.name">
  909. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">name</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a></em><a class="headerlink" href="#tarfile.TarInfo.name" title="Link to this definition">¶</a></dt>
  910. <dd><p>Name of the archive member.</p>
  911. </dd></dl>
  912. <dl class="py attribute">
  913. <dt class="sig sig-object py" id="tarfile.TarInfo.size">
  914. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">size</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarInfo.size" title="Link to this definition">¶</a></dt>
  915. <dd><p>Size in bytes.</p>
  916. </dd></dl>
  917. <dl class="py attribute">
  918. <dt class="sig sig-object py" id="tarfile.TarInfo.mtime">
  919. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">mtime</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><a class="reference internal" href="functions.html#float" title="float"><span class="pre">float</span></a></em><a class="headerlink" href="#tarfile.TarInfo.mtime" title="Link to this definition">¶</a></dt>
  920. <dd><p>Time of last modification in seconds since the <a class="reference internal" href="time.html#epoch"><span class="std std-ref">epoch</span></a>,
  921. as in <a class="reference internal" href="os.html#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">os.stat_result.st_mtime</span></code></a>.</p>
  922. <div class="versionchanged">
  923. <p><span class="versionmodified changed">Changed in version 3.12: </span>Can be set to <code class="docutils literal notranslate"><span class="pre">None</span></code> for <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> and
  924. <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>, causing extraction to skip applying this
  925. attribute.</p>
  926. </div>
  927. </dd></dl>
  928. <dl class="py attribute">
  929. <dt class="sig sig-object py" id="tarfile.TarInfo.mode">
  930. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">mode</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarInfo.mode" title="Link to this definition">¶</a></dt>
  931. <dd><p>Permission bits, as for <a class="reference internal" href="os.html#os.chmod" title="os.chmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.chmod()</span></code></a>.</p>
  932. <div class="versionchanged">
  933. <p><span class="versionmodified changed">Changed in version 3.12: </span>Can be set to <code class="docutils literal notranslate"><span class="pre">None</span></code> for <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> and
  934. <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>, causing extraction to skip applying this
  935. attribute.</p>
  936. </div>
  937. </dd></dl>
  938. <dl class="py attribute">
  939. <dt class="sig sig-object py" id="tarfile.TarInfo.type">
  940. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">type</span></span><a class="headerlink" href="#tarfile.TarInfo.type" title="Link to this definition">¶</a></dt>
  941. <dd><p>File type. <em>type</em> is usually one of these constants: <a class="reference internal" href="#tarfile.REGTYPE" title="tarfile.REGTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">REGTYPE</span></code></a>,
  942. <a class="reference internal" href="#tarfile.AREGTYPE" title="tarfile.AREGTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">AREGTYPE</span></code></a>, <a class="reference internal" href="#tarfile.LNKTYPE" title="tarfile.LNKTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">LNKTYPE</span></code></a>, <a class="reference internal" href="#tarfile.SYMTYPE" title="tarfile.SYMTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">SYMTYPE</span></code></a>, <a class="reference internal" href="#tarfile.DIRTYPE" title="tarfile.DIRTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">DIRTYPE</span></code></a>,
  943. <a class="reference internal" href="#tarfile.FIFOTYPE" title="tarfile.FIFOTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">FIFOTYPE</span></code></a>, <a class="reference internal" href="#tarfile.CONTTYPE" title="tarfile.CONTTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">CONTTYPE</span></code></a>, <a class="reference internal" href="#tarfile.CHRTYPE" title="tarfile.CHRTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">CHRTYPE</span></code></a>, <a class="reference internal" href="#tarfile.BLKTYPE" title="tarfile.BLKTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">BLKTYPE</span></code></a>,
  944. <a class="reference internal" href="#tarfile.GNUTYPE_SPARSE" title="tarfile.GNUTYPE_SPARSE"><code class="xref py py-const docutils literal notranslate"><span class="pre">GNUTYPE_SPARSE</span></code></a>. To determine the type of a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object
  945. more conveniently, use the <code class="docutils literal notranslate"><span class="pre">is*()</span></code> methods below.</p>
  946. </dd></dl>
  947. <dl class="py attribute">
  948. <dt class="sig sig-object py" id="tarfile.TarInfo.linkname">
  949. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">linkname</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a></em><a class="headerlink" href="#tarfile.TarInfo.linkname" title="Link to this definition">¶</a></dt>
  950. <dd><p>Name of the target file name, which is only present in <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> objects
  951. of type <a class="reference internal" href="#tarfile.LNKTYPE" title="tarfile.LNKTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">LNKTYPE</span></code></a> and <a class="reference internal" href="#tarfile.SYMTYPE" title="tarfile.SYMTYPE"><code class="xref py py-const docutils literal notranslate"><span class="pre">SYMTYPE</span></code></a>.</p>
  952. <p>For symbolic links (<code class="docutils literal notranslate"><span class="pre">SYMTYPE</span></code>), the <em>linkname</em> is relative to the directory
  953. that contains the link.
  954. For hard links (<code class="docutils literal notranslate"><span class="pre">LNKTYPE</span></code>), the <em>linkname</em> is relative to the root of
  955. the archive.</p>
  956. </dd></dl>
  957. <dl class="py attribute">
  958. <dt class="sig sig-object py" id="tarfile.TarInfo.uid">
  959. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">uid</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarInfo.uid" title="Link to this definition">¶</a></dt>
  960. <dd><p>User ID of the user who originally stored this member.</p>
  961. <div class="versionchanged">
  962. <p><span class="versionmodified changed">Changed in version 3.12: </span>Can be set to <code class="docutils literal notranslate"><span class="pre">None</span></code> for <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> and
  963. <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>, causing extraction to skip applying this
  964. attribute.</p>
  965. </div>
  966. </dd></dl>
  967. <dl class="py attribute">
  968. <dt class="sig sig-object py" id="tarfile.TarInfo.gid">
  969. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">gid</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarInfo.gid" title="Link to this definition">¶</a></dt>
  970. <dd><p>Group ID of the user who originally stored this member.</p>
  971. <div class="versionchanged">
  972. <p><span class="versionmodified changed">Changed in version 3.12: </span>Can be set to <code class="docutils literal notranslate"><span class="pre">None</span></code> for <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> and
  973. <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>, causing extraction to skip applying this
  974. attribute.</p>
  975. </div>
  976. </dd></dl>
  977. <dl class="py attribute">
  978. <dt class="sig sig-object py" id="tarfile.TarInfo.uname">
  979. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">uname</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a></em><a class="headerlink" href="#tarfile.TarInfo.uname" title="Link to this definition">¶</a></dt>
  980. <dd><p>User name.</p>
  981. <div class="versionchanged">
  982. <p><span class="versionmodified changed">Changed in version 3.12: </span>Can be set to <code class="docutils literal notranslate"><span class="pre">None</span></code> for <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> and
  983. <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>, causing extraction to skip applying this
  984. attribute.</p>
  985. </div>
  986. </dd></dl>
  987. <dl class="py attribute">
  988. <dt class="sig sig-object py" id="tarfile.TarInfo.gname">
  989. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">gname</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a></em><a class="headerlink" href="#tarfile.TarInfo.gname" title="Link to this definition">¶</a></dt>
  990. <dd><p>Group name.</p>
  991. <div class="versionchanged">
  992. <p><span class="versionmodified changed">Changed in version 3.12: </span>Can be set to <code class="docutils literal notranslate"><span class="pre">None</span></code> for <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extract()</span></code></a> and
  993. <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>, causing extraction to skip applying this
  994. attribute.</p>
  995. </div>
  996. </dd></dl>
  997. <dl class="py attribute">
  998. <dt class="sig sig-object py" id="tarfile.TarInfo.chksum">
  999. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">chksum</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarInfo.chksum" title="Link to this definition">¶</a></dt>
  1000. <dd><p>Header checksum.</p>
  1001. </dd></dl>
  1002. <dl class="py attribute">
  1003. <dt class="sig sig-object py" id="tarfile.TarInfo.devmajor">
  1004. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">devmajor</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarInfo.devmajor" title="Link to this definition">¶</a></dt>
  1005. <dd><p>Device major number.</p>
  1006. </dd></dl>
  1007. <dl class="py attribute">
  1008. <dt class="sig sig-object py" id="tarfile.TarInfo.devminor">
  1009. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">devminor</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarInfo.devminor" title="Link to this definition">¶</a></dt>
  1010. <dd><p>Device minor number.</p>
  1011. </dd></dl>
  1012. <dl class="py attribute">
  1013. <dt class="sig sig-object py" id="tarfile.TarInfo.offset">
  1014. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">offset</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarInfo.offset" title="Link to this definition">¶</a></dt>
  1015. <dd><p>The tar header starts here.</p>
  1016. </dd></dl>
  1017. <dl class="py attribute">
  1018. <dt class="sig sig-object py" id="tarfile.TarInfo.offset_data">
  1019. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">offset_data</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></em><a class="headerlink" href="#tarfile.TarInfo.offset_data" title="Link to this definition">¶</a></dt>
  1020. <dd><p>The file’s data starts here.</p>
  1021. </dd></dl>
  1022. <dl class="py attribute">
  1023. <dt class="sig sig-object py" id="tarfile.TarInfo.sparse">
  1024. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">sparse</span></span><a class="headerlink" href="#tarfile.TarInfo.sparse" title="Link to this definition">¶</a></dt>
  1025. <dd><p>Sparse member information.</p>
  1026. </dd></dl>
  1027. <dl class="py attribute">
  1028. <dt class="sig sig-object py" id="tarfile.TarInfo.pax_headers">
  1029. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">pax_headers</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="stdtypes.html#dict" title="dict"><span class="pre">dict</span></a></em><a class="headerlink" href="#tarfile.TarInfo.pax_headers" title="Link to this definition">¶</a></dt>
  1030. <dd><p>A dictionary containing key-value pairs of an associated pax extended header.</p>
  1031. </dd></dl>
  1032. <dl class="py method">
  1033. <dt class="sig sig-object py" id="tarfile.TarInfo.replace">
  1034. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">replace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</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">mtime</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">mode</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">linkname</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">uid</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">gid</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">uname</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">gname</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">deep</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="#tarfile.TarInfo.replace" title="Link to this definition">¶</a></dt>
  1035. <dd><div class="versionadded">
  1036. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1037. </div>
  1038. <p>Return a <em>new</em> copy of the <code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code> object with the given attributes
  1039. changed. For example, to return a <code class="docutils literal notranslate"><span class="pre">TarInfo</span></code> with the group name set to
  1040. <code class="docutils literal notranslate"><span class="pre">'staff'</span></code>, use:</p>
  1041. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">new_tarinfo</span> <span class="o">=</span> <span class="n">old_tarinfo</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">gname</span><span class="o">=</span><span class="s1">&#39;staff&#39;</span><span class="p">)</span>
  1042. </pre></div>
  1043. </div>
  1044. <p>By default, a deep copy is made.
  1045. If <em>deep</em> is false, the copy is shallow, i.e. <code class="docutils literal notranslate"><span class="pre">pax_headers</span></code>
  1046. and any custom attributes are shared with the original <code class="docutils literal notranslate"><span class="pre">TarInfo</span></code> object.</p>
  1047. </dd></dl>
  1048. <p>A <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object also provides some convenient query methods:</p>
  1049. <dl class="py method">
  1050. <dt class="sig sig-object py" id="tarfile.TarInfo.isfile">
  1051. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">isfile</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.isfile" title="Link to this definition">¶</a></dt>
  1052. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if the <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object is a regular file.</p>
  1053. </dd></dl>
  1054. <dl class="py method">
  1055. <dt class="sig sig-object py" id="tarfile.TarInfo.isreg">
  1056. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">isreg</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.isreg" title="Link to this definition">¶</a></dt>
  1057. <dd><p>Same as <a class="reference internal" href="#tarfile.TarInfo.isfile" title="tarfile.TarInfo.isfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isfile()</span></code></a>.</p>
  1058. </dd></dl>
  1059. <dl class="py method">
  1060. <dt class="sig sig-object py" id="tarfile.TarInfo.isdir">
  1061. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">isdir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.isdir" title="Link to this definition">¶</a></dt>
  1062. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if it is a directory.</p>
  1063. </dd></dl>
  1064. <dl class="py method">
  1065. <dt class="sig sig-object py" id="tarfile.TarInfo.issym">
  1066. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">issym</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.issym" title="Link to this definition">¶</a></dt>
  1067. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if it is a symbolic link.</p>
  1068. </dd></dl>
  1069. <dl class="py method">
  1070. <dt class="sig sig-object py" id="tarfile.TarInfo.islnk">
  1071. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">islnk</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.islnk" title="Link to this definition">¶</a></dt>
  1072. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if it is a hard link.</p>
  1073. </dd></dl>
  1074. <dl class="py method">
  1075. <dt class="sig sig-object py" id="tarfile.TarInfo.ischr">
  1076. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">ischr</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.ischr" title="Link to this definition">¶</a></dt>
  1077. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if it is a character device.</p>
  1078. </dd></dl>
  1079. <dl class="py method">
  1080. <dt class="sig sig-object py" id="tarfile.TarInfo.isblk">
  1081. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">isblk</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.isblk" title="Link to this definition">¶</a></dt>
  1082. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if it is a block device.</p>
  1083. </dd></dl>
  1084. <dl class="py method">
  1085. <dt class="sig sig-object py" id="tarfile.TarInfo.isfifo">
  1086. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">isfifo</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.isfifo" title="Link to this definition">¶</a></dt>
  1087. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if it is a FIFO.</p>
  1088. </dd></dl>
  1089. <dl class="py method">
  1090. <dt class="sig sig-object py" id="tarfile.TarInfo.isdev">
  1091. <span class="sig-prename descclassname"><span class="pre">TarInfo.</span></span><span class="sig-name descname"><span class="pre">isdev</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.TarInfo.isdev" title="Link to this definition">¶</a></dt>
  1092. <dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> if it is one of character device, block device or FIFO.</p>
  1093. </dd></dl>
  1094. </section>
  1095. <section id="extraction-filters">
  1096. <span id="tarfile-extraction-filter"></span><h2>Extraction filters<a class="headerlink" href="#extraction-filters" title="Link to this heading">¶</a></h2>
  1097. <div class="versionadded">
  1098. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1099. </div>
  1100. <p>The <em>tar</em> format is designed to capture all details of a UNIX-like filesystem,
  1101. which makes it very powerful.
  1102. Unfortunately, the features make it easy to create tar files that have
  1103. unintended – and possibly malicious – effects when extracted.
  1104. For example, extracting a tar file can overwrite arbitrary files in various
  1105. ways (e.g. by using absolute paths, <code class="docutils literal notranslate"><span class="pre">..</span></code> path components, or symlinks that
  1106. affect later members).</p>
  1107. <p>In most cases, the full functionality is not needed.
  1108. Therefore, <em>tarfile</em> supports extraction filters: a mechanism to limit
  1109. functionality, and thus mitigate some of the security issues.</p>
  1110. <div class="admonition seealso">
  1111. <p class="admonition-title">See also</p>
  1112. <dl class="simple">
  1113. <dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0706/"><strong>PEP 706</strong></a></dt><dd><p>Contains further motivation and rationale behind the design.</p>
  1114. </dd>
  1115. </dl>
  1116. </div>
  1117. <p>The <em>filter</em> argument to <a class="reference internal" href="#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.extract()</span></code></a> or <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a>
  1118. can be:</p>
  1119. <ul>
  1120. <li><p>the string <code class="docutils literal notranslate"><span class="pre">'fully_trusted'</span></code>: Honor all metadata as specified in the
  1121. archive.
  1122. Should be used if the user trusts the archive completely, or implements
  1123. their own complex verification.</p></li>
  1124. <li><p>the string <code class="docutils literal notranslate"><span class="pre">'tar'</span></code>: Honor most <em>tar</em>-specific features (i.e. features of
  1125. UNIX-like filesystems), but block features that are very likely to be
  1126. surprising or malicious. See <a class="reference internal" href="#tarfile.tar_filter" title="tarfile.tar_filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">tar_filter()</span></code></a> for details.</p></li>
  1127. <li><p>the string <code class="docutils literal notranslate"><span class="pre">'data'</span></code>: Ignore or block most features specific to UNIX-like
  1128. filesystems. Intended for extracting cross-platform data archives.
  1129. See <a class="reference internal" href="#tarfile.data_filter" title="tarfile.data_filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">data_filter()</span></code></a> for details.</p></li>
  1130. <li><p><code class="docutils literal notranslate"><span class="pre">None</span></code> (default): Use <a class="reference internal" href="#tarfile.TarFile.extraction_filter" title="tarfile.TarFile.extraction_filter"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TarFile.extraction_filter</span></code></a>.</p>
  1131. <p>If that is also <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), raise a <code class="docutils literal notranslate"><span class="pre">DeprecationWarning</span></code>,
  1132. and fall back to the <code class="docutils literal notranslate"><span class="pre">'fully_trusted'</span></code> filter, whose dangerous behavior
  1133. matches previous versions of Python.</p>
  1134. <p>In Python 3.14, the <code class="docutils literal notranslate"><span class="pre">'data'</span></code> filter will become the default instead.
  1135. It’s possible to switch earlier; see <a class="reference internal" href="#tarfile.TarFile.extraction_filter" title="tarfile.TarFile.extraction_filter"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TarFile.extraction_filter</span></code></a>.</p>
  1136. </li>
  1137. <li><p>A callable which will be called for each extracted member with a
  1138. <a class="reference internal" href="#tarinfo-objects"><span class="std std-ref">TarInfo</span></a> describing the member and the destination
  1139. path to where the archive is extracted (i.e. the same path is used for all
  1140. members):</p>
  1141. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">filter</span><span class="p">(</span><span class="n">member</span><span class="p">:</span> <span class="n">TarInfo</span><span class="p">,</span> <span class="n">path</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="o">/</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">TarInfo</span> <span class="o">|</span> <span class="kc">None</span>
  1142. </pre></div>
  1143. </div>
  1144. <p>The callable is called just before each member is extracted, so it can
  1145. take the current state of the disk into account.
  1146. It can:</p>
  1147. <ul class="simple">
  1148. <li><p>return a <a class="reference internal" href="#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> object which will be used instead of the metadata
  1149. in the archive, or</p></li>
  1150. <li><p>return <code class="docutils literal notranslate"><span class="pre">None</span></code>, in which case the member will be skipped, or</p></li>
  1151. <li><p>raise an exception to abort the operation or skip the member,
  1152. depending on <a class="reference internal" href="#tarfile.TarFile.errorlevel" title="tarfile.TarFile.errorlevel"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errorlevel</span></code></a>.
  1153. Note that when extraction is aborted, <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extractall()</span></code></a> may leave
  1154. the archive partially extracted. It does not attempt to clean up.</p></li>
  1155. </ul>
  1156. </li>
  1157. </ul>
  1158. <section id="default-named-filters">
  1159. <h3>Default named filters<a class="headerlink" href="#default-named-filters" title="Link to this heading">¶</a></h3>
  1160. <p>The pre-defined, named filters are available as functions, so they can be
  1161. reused in custom filters:</p>
  1162. <dl class="py function">
  1163. <dt class="sig sig-object py" id="tarfile.fully_trusted_filter">
  1164. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">fully_trusted_filter</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></em><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.fully_trusted_filter" title="Link to this definition">¶</a></dt>
  1165. <dd><p>Return <em>member</em> unchanged.</p>
  1166. <p>This implements the <code class="docutils literal notranslate"><span class="pre">'fully_trusted'</span></code> filter.</p>
  1167. </dd></dl>
  1168. <dl class="py function">
  1169. <dt class="sig sig-object py" id="tarfile.tar_filter">
  1170. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">tar_filter</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></em><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.tar_filter" title="Link to this definition">¶</a></dt>
  1171. <dd><p>Implements the <code class="docutils literal notranslate"><span class="pre">'tar'</span></code> filter.</p>
  1172. <ul class="simple">
  1173. <li><p>Strip leading slashes (<code class="docutils literal notranslate"><span class="pre">/</span></code> and <a class="reference internal" href="os.html#os.sep" title="os.sep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.sep</span></code></a>) from filenames.</p></li>
  1174. <li><p><a class="reference internal" href="#tarfile-extraction-refuse"><span class="std std-ref">Refuse</span></a> to extract files with absolute
  1175. paths (in case the name is absolute
  1176. even after stripping slashes, e.g. <code class="docutils literal notranslate"><span class="pre">C:/foo</span></code> on Windows).
  1177. This raises <a class="reference internal" href="#tarfile.AbsolutePathError" title="tarfile.AbsolutePathError"><code class="xref py py-class docutils literal notranslate"><span class="pre">AbsolutePathError</span></code></a>.</p></li>
  1178. <li><p><a class="reference internal" href="#tarfile-extraction-refuse"><span class="std std-ref">Refuse</span></a> to extract files whose absolute
  1179. path (after following symlinks) would end up outside the destination.
  1180. This raises <a class="reference internal" href="#tarfile.OutsideDestinationError" title="tarfile.OutsideDestinationError"><code class="xref py py-class docutils literal notranslate"><span class="pre">OutsideDestinationError</span></code></a>.</p></li>
  1181. <li><p>Clear high mode bits (setuid, setgid, sticky) and group/other write bits
  1182. (<a class="reference internal" href="stat.html#stat.S_IWGRP" title="stat.S_IWGRP"><code class="xref py py-const docutils literal notranslate"><span class="pre">S_IWGRP</span></code></a> | <a class="reference internal" href="stat.html#stat.S_IWOTH" title="stat.S_IWOTH"><code class="xref py py-const docutils literal notranslate"><span class="pre">S_IWOTH</span></code></a>).</p></li>
  1183. </ul>
  1184. <p>Return the modified <code class="docutils literal notranslate"><span class="pre">TarInfo</span></code> member.</p>
  1185. </dd></dl>
  1186. <dl class="py function">
  1187. <dt class="sig sig-object py" id="tarfile.data_filter">
  1188. <span class="sig-prename descclassname"><span class="pre">tarfile.</span></span><span class="sig-name descname"><span class="pre">data_filter</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></em><span class="sig-paren">)</span><a class="headerlink" href="#tarfile.data_filter" title="Link to this definition">¶</a></dt>
  1189. <dd><p>Implements the <code class="docutils literal notranslate"><span class="pre">'data'</span></code> filter.
  1190. In addition to what <code class="docutils literal notranslate"><span class="pre">tar_filter</span></code> does:</p>
  1191. <ul>
  1192. <li><p><a class="reference internal" href="#tarfile-extraction-refuse"><span class="std std-ref">Refuse</span></a> to extract links (hard or soft)
  1193. that link to absolute paths, or ones that link outside the destination.</p>
  1194. <p>This raises <a class="reference internal" href="#tarfile.AbsoluteLinkError" title="tarfile.AbsoluteLinkError"><code class="xref py py-class docutils literal notranslate"><span class="pre">AbsoluteLinkError</span></code></a> or
  1195. <a class="reference internal" href="#tarfile.LinkOutsideDestinationError" title="tarfile.LinkOutsideDestinationError"><code class="xref py py-class docutils literal notranslate"><span class="pre">LinkOutsideDestinationError</span></code></a>.</p>
  1196. <p>Note that such files are refused even on platforms that do not support
  1197. symbolic links.</p>
  1198. </li>
  1199. <li><p><a class="reference internal" href="#tarfile-extraction-refuse"><span class="std std-ref">Refuse</span></a> to extract device files
  1200. (including pipes).
  1201. This raises <a class="reference internal" href="#tarfile.SpecialFileError" title="tarfile.SpecialFileError"><code class="xref py py-class docutils literal notranslate"><span class="pre">SpecialFileError</span></code></a>.</p></li>
  1202. <li><p>For regular files, including hard links:</p>
  1203. <ul class="simple">
  1204. <li><p>Set the owner read and write permissions
  1205. (<a class="reference internal" href="stat.html#stat.S_IRUSR" title="stat.S_IRUSR"><code class="xref py py-const docutils literal notranslate"><span class="pre">S_IRUSR</span></code></a> | <a class="reference internal" href="stat.html#stat.S_IWUSR" title="stat.S_IWUSR"><code class="xref py py-const docutils literal notranslate"><span class="pre">S_IWUSR</span></code></a>).</p></li>
  1206. <li><p>Remove the group &amp; other executable permission
  1207. (<a class="reference internal" href="stat.html#stat.S_IXGRP" title="stat.S_IXGRP"><code class="xref py py-const docutils literal notranslate"><span class="pre">S_IXGRP</span></code></a> | <a class="reference internal" href="stat.html#stat.S_IXOTH" title="stat.S_IXOTH"><code class="xref py py-const docutils literal notranslate"><span class="pre">S_IXOTH</span></code></a>)
  1208. if the owner doesn’t have it (<a class="reference internal" href="stat.html#stat.S_IXUSR" title="stat.S_IXUSR"><code class="xref py py-const docutils literal notranslate"><span class="pre">S_IXUSR</span></code></a>).</p></li>
  1209. </ul>
  1210. </li>
  1211. <li><p>For other files (directories), set <code class="docutils literal notranslate"><span class="pre">mode</span></code> to <code class="docutils literal notranslate"><span class="pre">None</span></code>, so
  1212. that extraction methods skip applying permission bits.</p></li>
  1213. <li><p>Set user and group info (<code class="docutils literal notranslate"><span class="pre">uid</span></code>, <code class="docutils literal notranslate"><span class="pre">gid</span></code>, <code class="docutils literal notranslate"><span class="pre">uname</span></code>, <code class="docutils literal notranslate"><span class="pre">gname</span></code>)
  1214. to <code class="docutils literal notranslate"><span class="pre">None</span></code>, so that extraction methods skip setting it.</p></li>
  1215. </ul>
  1216. <p>Return the modified <code class="docutils literal notranslate"><span class="pre">TarInfo</span></code> member.</p>
  1217. </dd></dl>
  1218. </section>
  1219. <section id="filter-errors">
  1220. <span id="tarfile-extraction-refuse"></span><h3>Filter errors<a class="headerlink" href="#filter-errors" title="Link to this heading">¶</a></h3>
  1221. <p>When a filter refuses to extract a file, it will raise an appropriate exception,
  1222. a subclass of <a class="reference internal" href="#tarfile.FilterError" title="tarfile.FilterError"><code class="xref py py-class docutils literal notranslate"><span class="pre">FilterError</span></code></a>.
  1223. This will abort the extraction if <a class="reference internal" href="#tarfile.TarFile.errorlevel" title="tarfile.TarFile.errorlevel"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TarFile.errorlevel</span></code></a> is 1 or more.
  1224. With <code class="docutils literal notranslate"><span class="pre">errorlevel=0</span></code> the error will be logged and the member will be skipped,
  1225. but extraction will continue.</p>
  1226. </section>
  1227. <section id="hints-for-further-verification">
  1228. <h3>Hints for further verification<a class="headerlink" href="#hints-for-further-verification" title="Link to this heading">¶</a></h3>
  1229. <p>Even with <code class="docutils literal notranslate"><span class="pre">filter='data'</span></code>, <em>tarfile</em> is not suited for extracting untrusted
  1230. files without prior inspection.
  1231. Among other issues, the pre-defined filters do not prevent denial-of-service
  1232. attacks. Users should do additional checks.</p>
  1233. <p>Here is an incomplete list of things to consider:</p>
  1234. <ul class="simple">
  1235. <li><p>Extract to a <a class="reference internal" href="tempfile.html#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">new</span> <span class="pre">temporary</span> <span class="pre">directory</span></code></a>
  1236. to prevent e.g. exploiting pre-existing links, and to make it easier to
  1237. clean up after a failed extraction.</p></li>
  1238. <li><p>When working with untrusted data, use external (e.g. OS-level) limits on
  1239. disk, memory and CPU usage.</p></li>
  1240. <li><p>Check filenames against an allow-list of characters
  1241. (to filter out control characters, confusables, foreign path separators,
  1242. etc.).</p></li>
  1243. <li><p>Check that filenames have expected extensions (discouraging files that
  1244. execute when you “click on them”, or extension-less files like Windows special device names).</p></li>
  1245. <li><p>Limit the number of extracted files, total size of extracted data,
  1246. filename length (including symlink length), and size of individual files.</p></li>
  1247. <li><p>Check for files that would be shadowed on case-insensitive filesystems.</p></li>
  1248. </ul>
  1249. <p>Also note that:</p>
  1250. <ul class="simple">
  1251. <li><p>Tar files may contain multiple versions of the same file.
  1252. Later ones are expected to overwrite any earlier ones.
  1253. This feature is crucial to allow updating tape archives, but can be abused
  1254. maliciously.</p></li>
  1255. <li><p><em>tarfile</em> does not protect against issues with “live” data,
  1256. e.g. an attacker tinkering with the destination (or source) directory while
  1257. extraction (or archiving) is in progress.</p></li>
  1258. </ul>
  1259. </section>
  1260. <section id="supporting-older-python-versions">
  1261. <h3>Supporting older Python versions<a class="headerlink" href="#supporting-older-python-versions" title="Link to this heading">¶</a></h3>
  1262. <p>Extraction filters were added to Python 3.12, but may be backported to older
  1263. versions as security updates.
  1264. To check whether the feature is available, use e.g.
  1265. <code class="docutils literal notranslate"><span class="pre">hasattr(tarfile,</span> <span class="pre">'data_filter')</span></code> rather than checking the Python version.</p>
  1266. <p>The following examples show how to support Python versions with and without
  1267. the feature.
  1268. Note that setting <code class="docutils literal notranslate"><span class="pre">extraction_filter</span></code> will affect any subsequent operations.</p>
  1269. <ul>
  1270. <li><p>Fully trusted archive:</p>
  1271. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">my_tarfile</span><span class="o">.</span><span class="n">extraction_filter</span> <span class="o">=</span> <span class="p">(</span><span class="k">lambda</span> <span class="n">member</span><span class="p">,</span> <span class="n">path</span><span class="p">:</span> <span class="n">member</span><span class="p">)</span>
  1272. <span class="n">my_tarfile</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>
  1273. </pre></div>
  1274. </div>
  1275. </li>
  1276. <li><p>Use the <code class="docutils literal notranslate"><span class="pre">'data'</span></code> filter if available, but revert to Python 3.11 behavior
  1277. (<code class="docutils literal notranslate"><span class="pre">'fully_trusted'</span></code>) if this feature is not available:</p>
  1278. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">my_tarfile</span><span class="o">.</span><span class="n">extraction_filter</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">tarfile</span><span class="p">,</span> <span class="s1">&#39;data_filter&#39;</span><span class="p">,</span>
  1279. <span class="p">(</span><span class="k">lambda</span> <span class="n">member</span><span class="p">,</span> <span class="n">path</span><span class="p">:</span> <span class="n">member</span><span class="p">))</span>
  1280. <span class="n">my_tarfile</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>
  1281. </pre></div>
  1282. </div>
  1283. </li>
  1284. <li><p>Use the <code class="docutils literal notranslate"><span class="pre">'data'</span></code> filter; <em>fail</em> if it is not available:</p>
  1285. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">my_tarfile</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="nb">filter</span><span class="o">=</span><span class="n">tarfile</span><span class="o">.</span><span class="n">data_filter</span><span class="p">)</span>
  1286. </pre></div>
  1287. </div>
  1288. <p>or:</p>
  1289. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">my_tarfile</span><span class="o">.</span><span class="n">extraction_filter</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">data_filter</span>
  1290. <span class="n">my_tarfile</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>
  1291. </pre></div>
  1292. </div>
  1293. </li>
  1294. <li><p>Use the <code class="docutils literal notranslate"><span class="pre">'data'</span></code> filter; <em>warn</em> if it is not available:</p>
  1295. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">tarfile</span><span class="p">,</span> <span class="s1">&#39;data_filter&#39;</span><span class="p">):</span>
  1296. <span class="n">my_tarfile</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="nb">filter</span><span class="o">=</span><span class="s1">&#39;data&#39;</span><span class="p">)</span>
  1297. <span class="k">else</span><span class="p">:</span>
  1298. <span class="c1"># remove this when no longer needed</span>
  1299. <span class="n">warn_the_user</span><span class="p">(</span><span class="s1">&#39;Extracting may be unsafe; consider updating Python&#39;</span><span class="p">)</span>
  1300. <span class="n">my_tarfile</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>
  1301. </pre></div>
  1302. </div>
  1303. </li>
  1304. </ul>
  1305. </section>
  1306. <section id="stateful-extraction-filter-example">
  1307. <h3>Stateful extraction filter example<a class="headerlink" href="#stateful-extraction-filter-example" title="Link to this heading">¶</a></h3>
  1308. <p>While <em>tarfile</em>’s extraction methods take a simple <em>filter</em> callable,
  1309. custom filters may be more complex objects with an internal state.
  1310. It may be useful to write these as context managers, to be used like this:</p>
  1311. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">StatefulFilter</span><span class="p">()</span> <span class="k">as</span> <span class="n">filter_func</span><span class="p">:</span>
  1312. <span class="n">tar</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="nb">filter</span><span class="o">=</span><span class="n">filter_func</span><span class="p">)</span>
  1313. </pre></div>
  1314. </div>
  1315. <p>Such a filter can be written as, for example:</p>
  1316. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">StatefulFilter</span><span class="p">:</span>
  1317. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1318. <span class="bp">self</span><span class="o">.</span><span class="n">file_count</span> <span class="o">=</span> <span class="mi">0</span>
  1319. <span class="k">def</span> <span class="fm">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1320. <span class="k">return</span> <span class="bp">self</span>
  1321. <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">member</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
  1322. <span class="bp">self</span><span class="o">.</span><span class="n">file_count</span> <span class="o">+=</span> <span class="mi">1</span>
  1323. <span class="k">return</span> <span class="n">member</span>
  1324. <span class="k">def</span> <span class="fm">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">exc_info</span><span class="p">):</span>
  1325. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">file_count</span><span class="si">}</span><span class="s1"> files extracted&#39;</span><span class="p">)</span>
  1326. </pre></div>
  1327. </div>
  1328. </section>
  1329. </section>
  1330. <section id="command-line-interface">
  1331. <span id="tarfile-commandline"></span><h2>Command-Line Interface<a class="headerlink" href="#command-line-interface" title="Link to this heading">¶</a></h2>
  1332. <div class="versionadded">
  1333. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1334. </div>
  1335. <p>The <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> module provides a simple command-line interface to interact
  1336. with tar archives.</p>
  1337. <p>If you want to create a new tar archive, specify its name after the <a class="reference internal" href="#cmdoption-tarfile-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a>
  1338. option and then list the filename(s) that should be included:</p>
  1339. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>tarfile<span class="w"> </span>-c<span class="w"> </span>monty.tar<span class="w"> </span>spam.txt<span class="w"> </span>eggs.txt
  1340. </pre></div>
  1341. </div>
  1342. <p>Passing a directory is also acceptable:</p>
  1343. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>tarfile<span class="w"> </span>-c<span class="w"> </span>monty.tar<span class="w"> </span>life-of-brian_1979/
  1344. </pre></div>
  1345. </div>
  1346. <p>If you want to extract a tar archive into the current directory, use
  1347. the <a class="reference internal" href="#cmdoption-tarfile-e"><code class="xref std std-option docutils literal notranslate"><span class="pre">-e</span></code></a> option:</p>
  1348. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>tarfile<span class="w"> </span>-e<span class="w"> </span>monty.tar
  1349. </pre></div>
  1350. </div>
  1351. <p>You can also extract a tar archive into a different directory by passing the
  1352. directory’s name:</p>
  1353. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>tarfile<span class="w"> </span>-e<span class="w"> </span>monty.tar<span class="w"> </span>other-dir/
  1354. </pre></div>
  1355. </div>
  1356. <p>For a list of the files in a tar archive, use the <a class="reference internal" href="#cmdoption-tarfile-l"><code class="xref std std-option docutils literal notranslate"><span class="pre">-l</span></code></a> option:</p>
  1357. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>tarfile<span class="w"> </span>-l<span class="w"> </span>monty.tar
  1358. </pre></div>
  1359. </div>
  1360. <section id="command-line-options">
  1361. <h3>Command-line options<a class="headerlink" href="#command-line-options" title="Link to this heading">¶</a></h3>
  1362. <dl class="std option">
  1363. <dt class="sig sig-object std" id="cmdoption-tarfile-l">
  1364. <span class="sig-name descname"><span class="pre">-l</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;tarfile&gt;</span></span><a class="headerlink" href="#cmdoption-tarfile-l" title="Link to this definition">¶</a></dt>
  1365. <dt class="sig sig-object std" id="cmdoption-tarfile-list">
  1366. <span class="sig-name descname"><span class="pre">--list</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;tarfile&gt;</span></span><a class="headerlink" href="#cmdoption-tarfile-list" title="Link to this definition">¶</a></dt>
  1367. <dd><p>List files in a tarfile.</p>
  1368. </dd></dl>
  1369. <dl class="std option">
  1370. <dt class="sig sig-object std" id="cmdoption-tarfile-c">
  1371. <span class="sig-name descname"><span class="pre">-c</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;tarfile&gt;</span> <span class="pre">&lt;source1&gt;</span> <span class="pre">...</span> <span class="pre">&lt;sourceN&gt;</span></span><a class="headerlink" href="#cmdoption-tarfile-c" title="Link to this definition">¶</a></dt>
  1372. <dt class="sig sig-object std" id="cmdoption-tarfile-create">
  1373. <span class="sig-name descname"><span class="pre">--create</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;tarfile&gt;</span> <span class="pre">&lt;source1&gt;</span> <span class="pre">...</span> <span class="pre">&lt;sourceN&gt;</span></span><a class="headerlink" href="#cmdoption-tarfile-create" title="Link to this definition">¶</a></dt>
  1374. <dd><p>Create tarfile from source files.</p>
  1375. </dd></dl>
  1376. <dl class="std option">
  1377. <dt class="sig sig-object std" id="cmdoption-tarfile-e">
  1378. <span class="sig-name descname"><span class="pre">-e</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;tarfile&gt;</span> <span class="pre">[&lt;output_dir&gt;]</span></span><a class="headerlink" href="#cmdoption-tarfile-e" title="Link to this definition">¶</a></dt>
  1379. <dt class="sig sig-object std" id="cmdoption-tarfile-extract">
  1380. <span class="sig-name descname"><span class="pre">--extract</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;tarfile&gt;</span> <span class="pre">[&lt;output_dir&gt;]</span></span><a class="headerlink" href="#cmdoption-tarfile-extract" title="Link to this definition">¶</a></dt>
  1381. <dd><p>Extract tarfile into the current directory if <em>output_dir</em> is not specified.</p>
  1382. </dd></dl>
  1383. <dl class="std option">
  1384. <dt class="sig sig-object std" id="cmdoption-tarfile-t">
  1385. <span class="sig-name descname"><span class="pre">-t</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;tarfile&gt;</span></span><a class="headerlink" href="#cmdoption-tarfile-t" title="Link to this definition">¶</a></dt>
  1386. <dt class="sig sig-object std" id="cmdoption-tarfile-test">
  1387. <span class="sig-name descname"><span class="pre">--test</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;tarfile&gt;</span></span><a class="headerlink" href="#cmdoption-tarfile-test" title="Link to this definition">¶</a></dt>
  1388. <dd><p>Test whether the tarfile is valid or not.</p>
  1389. </dd></dl>
  1390. <dl class="std option">
  1391. <dt class="sig sig-object std" id="cmdoption-tarfile-v">
  1392. <span id="cmdoption-tarfile-verbose"></span><span class="sig-name descname"><span class="pre">-v</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--verbose</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-tarfile-v" title="Link to this definition">¶</a></dt>
  1393. <dd><p>Verbose output.</p>
  1394. </dd></dl>
  1395. <dl class="std option">
  1396. <dt class="sig sig-object std" id="cmdoption-tarfile-filter">
  1397. <span class="sig-name descname"><span class="pre">--filter</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;filtername&gt;</span></span><a class="headerlink" href="#cmdoption-tarfile-filter" title="Link to this definition">¶</a></dt>
  1398. <dd><p>Specifies the <em>filter</em> for <code class="docutils literal notranslate"><span class="pre">--extract</span></code>.
  1399. See <a class="reference internal" href="#tarfile-extraction-filter"><span class="std std-ref">Extraction filters</span></a> for details.
  1400. Only string names are accepted (that is, <code class="docutils literal notranslate"><span class="pre">fully_trusted</span></code>, <code class="docutils literal notranslate"><span class="pre">tar</span></code>,
  1401. and <code class="docutils literal notranslate"><span class="pre">data</span></code>).</p>
  1402. </dd></dl>
  1403. </section>
  1404. </section>
  1405. <section id="examples">
  1406. <span id="tar-examples"></span><h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  1407. <p>How to extract an entire tar archive to the current working directory:</p>
  1408. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
  1409. <span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;sample.tar.gz&quot;</span><span class="p">)</span>
  1410. <span class="n">tar</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="nb">filter</span><span class="o">=</span><span class="s1">&#39;data&#39;</span><span class="p">)</span>
  1411. <span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1412. </pre></div>
  1413. </div>
  1414. <p>How to extract a subset of a tar archive with <a class="reference internal" href="#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.extractall()</span></code></a> using
  1415. a generator function instead of a list:</p>
  1416. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
  1417. <span class="kn">import</span> <span class="nn">tarfile</span>
  1418. <span class="k">def</span> <span class="nf">py_files</span><span class="p">(</span><span class="n">members</span><span class="p">):</span>
  1419. <span class="k">for</span> <span class="n">tarinfo</span> <span class="ow">in</span> <span class="n">members</span><span class="p">:</span>
  1420. <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">tarinfo</span><span class="o">.</span><span class="n">name</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;.py&quot;</span><span class="p">:</span>
  1421. <span class="k">yield</span> <span class="n">tarinfo</span>
  1422. <span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;sample.tar.gz&quot;</span><span class="p">)</span>
  1423. <span class="n">tar</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="n">members</span><span class="o">=</span><span class="n">py_files</span><span class="p">(</span><span class="n">tar</span><span class="p">))</span>
  1424. <span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1425. </pre></div>
  1426. </div>
  1427. <p>How to create an uncompressed tar archive from a list of filenames:</p>
  1428. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
  1429. <span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;sample.tar&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span>
  1430. <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">,</span> <span class="s2">&quot;bar&quot;</span><span class="p">,</span> <span class="s2">&quot;quux&quot;</span><span class="p">]:</span>
  1431. <span class="n">tar</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
  1432. <span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1433. </pre></div>
  1434. </div>
  1435. <p>The same example using the <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>
  1436. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
  1437. <span class="k">with</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;sample.tar&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">tar</span><span class="p">:</span>
  1438. <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">,</span> <span class="s2">&quot;bar&quot;</span><span class="p">,</span> <span class="s2">&quot;quux&quot;</span><span class="p">]:</span>
  1439. <span class="n">tar</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
  1440. </pre></div>
  1441. </div>
  1442. <p>How to read a gzip compressed tar archive and display some member information:</p>
  1443. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
  1444. <span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;sample.tar.gz&quot;</span><span class="p">,</span> <span class="s2">&quot;r:gz&quot;</span><span class="p">)</span>
  1445. <span class="k">for</span> <span class="n">tarinfo</span> <span class="ow">in</span> <span class="n">tar</span><span class="p">:</span>
  1446. <span class="nb">print</span><span class="p">(</span><span class="n">tarinfo</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="s2">&quot;is&quot;</span><span class="p">,</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="s2">&quot;bytes in size and is &quot;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
  1447. <span class="k">if</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">isreg</span><span class="p">():</span>
  1448. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;a regular file.&quot;</span><span class="p">)</span>
  1449. <span class="k">elif</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">isdir</span><span class="p">():</span>
  1450. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;a directory.&quot;</span><span class="p">)</span>
  1451. <span class="k">else</span><span class="p">:</span>
  1452. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;something else.&quot;</span><span class="p">)</span>
  1453. <span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1454. </pre></div>
  1455. </div>
  1456. <p>How to create an archive and reset the user information using the <em>filter</em>
  1457. parameter in <a class="reference internal" href="#tarfile.TarFile.add" title="tarfile.TarFile.add"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.add()</span></code></a>:</p>
  1458. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tarfile</span>
  1459. <span class="k">def</span> <span class="nf">reset</span><span class="p">(</span><span class="n">tarinfo</span><span class="p">):</span>
  1460. <span class="n">tarinfo</span><span class="o">.</span><span class="n">uid</span> <span class="o">=</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">gid</span> <span class="o">=</span> <span class="mi">0</span>
  1461. <span class="n">tarinfo</span><span class="o">.</span><span class="n">uname</span> <span class="o">=</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">gname</span> <span class="o">=</span> <span class="s2">&quot;root&quot;</span>
  1462. <span class="k">return</span> <span class="n">tarinfo</span>
  1463. <span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;sample.tar.gz&quot;</span><span class="p">,</span> <span class="s2">&quot;w:gz&quot;</span><span class="p">)</span>
  1464. <span class="n">tar</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s2">&quot;foo&quot;</span><span class="p">,</span> <span class="nb">filter</span><span class="o">=</span><span class="n">reset</span><span class="p">)</span>
  1465. <span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1466. </pre></div>
  1467. </div>
  1468. </section>
  1469. <section id="supported-tar-formats">
  1470. <span id="tar-formats"></span><h2>Supported tar formats<a class="headerlink" href="#supported-tar-formats" title="Link to this heading">¶</a></h2>
  1471. <p>There are three tar formats that can be created with the <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> module:</p>
  1472. <ul>
  1473. <li><p>The POSIX.1-1988 ustar format (<a class="reference internal" href="#tarfile.USTAR_FORMAT" title="tarfile.USTAR_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">USTAR_FORMAT</span></code></a>). It supports filenames
  1474. up to a length of at best 256 characters and linknames up to 100 characters.
  1475. The maximum file size is 8 GiB. This is an old and limited but widely
  1476. supported format.</p></li>
  1477. <li><p>The GNU tar format (<a class="reference internal" href="#tarfile.GNU_FORMAT" title="tarfile.GNU_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">GNU_FORMAT</span></code></a>). It supports long filenames and
  1478. linknames, files bigger than 8 GiB and sparse files. It is the de facto
  1479. standard on GNU/Linux systems. <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> fully supports the GNU tar
  1480. extensions for long names, sparse file support is read-only.</p></li>
  1481. <li><p>The POSIX.1-2001 pax format (<a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PAX_FORMAT</span></code></a>). It is the most flexible
  1482. format with virtually no limits. It supports long filenames and linknames, large
  1483. files and stores pathnames in a portable way. Modern tar implementations,
  1484. including GNU tar, bsdtar/libarchive and star, fully support extended <em>pax</em>
  1485. features; some old or unmaintained libraries may not, but should treat
  1486. <em>pax</em> archives as if they were in the universally supported <em>ustar</em> format.
  1487. It is the current default format for new archives.</p>
  1488. <p>It extends the existing <em>ustar</em> format with extra headers for information
  1489. that cannot be stored otherwise. There are two flavours of pax headers:
  1490. Extended headers only affect the subsequent file header, global
  1491. headers are valid for the complete archive and affect all following files.
  1492. All the data in a pax header is encoded in <em>UTF-8</em> for portability reasons.</p>
  1493. </li>
  1494. </ul>
  1495. <p>There are some more variants of the tar format which can be read, but not
  1496. created:</p>
  1497. <ul class="simple">
  1498. <li><p>The ancient V7 format. This is the first tar format from Unix Seventh Edition,
  1499. storing only regular files and directories. Names must not be longer than 100
  1500. characters, there is no user/group name information. Some archives have
  1501. miscalculated header checksums in case of fields with non-ASCII characters.</p></li>
  1502. <li><p>The SunOS tar extended format. This format is a variant of the POSIX.1-2001
  1503. pax format, but is not compatible.</p></li>
  1504. </ul>
  1505. </section>
  1506. <section id="unicode-issues">
  1507. <span id="tar-unicode"></span><h2>Unicode issues<a class="headerlink" href="#unicode-issues" title="Link to this heading">¶</a></h2>
  1508. <p>The tar format was originally conceived to make backups on tape drives with the
  1509. main focus on preserving file system information. Nowadays tar archives are
  1510. commonly used for file distribution and exchanging archives over networks. One
  1511. problem of the original format (which is the basis of all other formats) is
  1512. that there is no concept of supporting different character encodings. For
  1513. example, an ordinary tar archive created on a <em>UTF-8</em> system cannot be read
  1514. correctly on a <em>Latin-1</em> system if it contains non-<em>ASCII</em> characters. Textual
  1515. metadata (like filenames, linknames, user/group names) will appear damaged.
  1516. Unfortunately, there is no way to autodetect the encoding of an archive. The
  1517. pax format was designed to solve this problem. It stores non-ASCII metadata
  1518. using the universal character encoding <em>UTF-8</em>.</p>
  1519. <p>The details of character conversion in <a class="reference internal" href="#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> are controlled by the
  1520. <em>encoding</em> and <em>errors</em> keyword arguments of the <a class="reference internal" href="#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> class.</p>
  1521. <p><em>encoding</em> defines the character encoding to use for the metadata in the
  1522. archive. The default value is <a class="reference internal" href="sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a> or <code class="docutils literal notranslate"><span class="pre">'ascii'</span></code>
  1523. as a fallback. Depending on whether the archive is read or written, the
  1524. metadata must be either decoded or encoded. If <em>encoding</em> is not set
  1525. appropriately, this conversion may fail.</p>
  1526. <p>The <em>errors</em> argument defines how characters are treated that cannot be
  1527. converted. Possible values are listed in section <a class="reference internal" href="codecs.html#error-handlers"><span class="std std-ref">Error Handlers</span></a>.
  1528. The default scheme is <code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> which Python also uses for its
  1529. file system calls, see <a class="reference internal" href="os.html#os-filenames"><span class="std std-ref">File Names, Command Line Arguments, and Environment Variables</span></a>.</p>
  1530. <p>For <a class="reference internal" href="#tarfile.PAX_FORMAT" title="tarfile.PAX_FORMAT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PAX_FORMAT</span></code></a> archives (the default), <em>encoding</em> is generally not needed
  1531. because all the metadata is stored using <em>UTF-8</em>. <em>encoding</em> is only used in
  1532. the rare cases when binary pax headers are decoded or when strings with
  1533. surrogate characters are stored.</p>
  1534. </section>
  1535. </section>
  1536. <div class="clearer"></div>
  1537. </div>
  1538. </div>
  1539. </div>
  1540. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1541. <div class="sphinxsidebarwrapper">
  1542. <div>
  1543. <h3><a href="../contents.html">Table of Contents</a></h3>
  1544. <ul>
  1545. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code> — Read and write tar archive files</a><ul>
  1546. <li><a class="reference internal" href="#tarfile-objects">TarFile Objects</a></li>
  1547. <li><a class="reference internal" href="#tarinfo-objects">TarInfo Objects</a></li>
  1548. <li><a class="reference internal" href="#extraction-filters">Extraction filters</a><ul>
  1549. <li><a class="reference internal" href="#default-named-filters">Default named filters</a></li>
  1550. <li><a class="reference internal" href="#filter-errors">Filter errors</a></li>
  1551. <li><a class="reference internal" href="#hints-for-further-verification">Hints for further verification</a></li>
  1552. <li><a class="reference internal" href="#supporting-older-python-versions">Supporting older Python versions</a></li>
  1553. <li><a class="reference internal" href="#stateful-extraction-filter-example">Stateful extraction filter example</a></li>
  1554. </ul>
  1555. </li>
  1556. <li><a class="reference internal" href="#command-line-interface">Command-Line Interface</a><ul>
  1557. <li><a class="reference internal" href="#command-line-options">Command-line options</a></li>
  1558. </ul>
  1559. </li>
  1560. <li><a class="reference internal" href="#examples">Examples</a></li>
  1561. <li><a class="reference internal" href="#supported-tar-formats">Supported tar formats</a></li>
  1562. <li><a class="reference internal" href="#unicode-issues">Unicode issues</a></li>
  1563. </ul>
  1564. </li>
  1565. </ul>
  1566. </div>
  1567. <div>
  1568. <h4>Previous topic</h4>
  1569. <p class="topless"><a href="zipfile.html"
  1570. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> — Work with ZIP archives</a></p>
  1571. </div>
  1572. <div>
  1573. <h4>Next topic</h4>
  1574. <p class="topless"><a href="fileformats.html"
  1575. title="next chapter">File Formats</a></p>
  1576. </div>
  1577. <div role="note" aria-label="source link">
  1578. <h3>This Page</h3>
  1579. <ul class="this-page-menu">
  1580. <li><a href="../bugs.html">Report a Bug</a></li>
  1581. <li>
  1582. <a href="https://github.com/python/cpython/blob/main/Doc/library/tarfile.rst"
  1583. rel="nofollow">Show Source
  1584. </a>
  1585. </li>
  1586. </ul>
  1587. </div>
  1588. </div>
  1589. <div id="sidebarbutton" title="Collapse sidebar">
  1590. <span>«</span>
  1591. </div>
  1592. </div>
  1593. <div class="clearer"></div>
  1594. </div>
  1595. <div class="related" role="navigation" aria-label="related navigation">
  1596. <h3>Navigation</h3>
  1597. <ul>
  1598. <li class="right" style="margin-right: 10px">
  1599. <a href="../genindex.html" title="General Index"
  1600. >index</a></li>
  1601. <li class="right" >
  1602. <a href="../py-modindex.html" title="Python Module Index"
  1603. >modules</a> |</li>
  1604. <li class="right" >
  1605. <a href="fileformats.html" title="File Formats"
  1606. >next</a> |</li>
  1607. <li class="right" >
  1608. <a href="zipfile.html" title="zipfile — Work with ZIP archives"
  1609. >previous</a> |</li>
  1610. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1611. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1612. <li class="switchers">
  1613. <div class="language_switcher_placeholder"></div>
  1614. <div class="version_switcher_placeholder"></div>
  1615. </li>
  1616. <li>
  1617. </li>
  1618. <li id="cpython-language-and-version">
  1619. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1620. </li>
  1621. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1622. <li class="nav-item nav-item-2"><a href="archiving.html" >Data Compression and Archiving</a> &#187;</li>
  1623. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code> — Read and write tar archive files</a></li>
  1624. <li class="right">
  1625. <div class="inline-search" role="search">
  1626. <form class="inline-search" action="../search.html" method="get">
  1627. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1628. <input type="submit" value="Go" />
  1629. </form>
  1630. </div>
  1631. |
  1632. </li>
  1633. <li class="right">
  1634. <label class="theme-selector-label">
  1635. Theme
  1636. <select class="theme-selector" oninput="activateTheme(this.value)">
  1637. <option value="auto" selected>Auto</option>
  1638. <option value="light">Light</option>
  1639. <option value="dark">Dark</option>
  1640. </select>
  1641. </label> |</li>
  1642. </ul>
  1643. </div>
  1644. <div class="footer">
  1645. &copy;
  1646. <a href="../copyright.html">
  1647. Copyright
  1648. </a>
  1649. 2001-2024, Python Software Foundation.
  1650. <br />
  1651. This page is licensed under the Python Software Foundation License Version 2.
  1652. <br />
  1653. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1654. <br />
  1655. See <a href="/license.html">History and License</a> for more information.<br />
  1656. <br />
  1657. The Python Software Foundation is a non-profit corporation.
  1658. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1659. <br />
  1660. <br />
  1661. Last updated on Apr 09, 2024 (13:47 UTC).
  1662. <a href="/bugs.html">Found a bug</a>?
  1663. <br />
  1664. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1665. </div>
  1666. </body>
  1667. </html>
上海开阖软件有限公司 沪ICP备12045867号-1