Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
shutil.html1233 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="shutil — High-level file operations" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/shutil.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/shutil.py The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_shutil_c04c71a4.png" />15<meta property="og:image:alt" content="Source code: Lib/shutil.py The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal..." />16<meta name="description" content="Source code: Lib/shutil.py The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>shutil — High-level file operations &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="Data Persistence" href="persistence.html" />43    <link rel="prev" title="linecache — Random access to text lines" href="linecache.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/shutil.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h3><a href="../contents.html">Table of Contents</a></h3>106    <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> — High-level file operations</a><ul>108<li><a class="reference internal" href="#directory-and-files-operations">Directory and files operations</a><ul>109<li><a class="reference internal" href="#platform-dependent-efficient-copy-operations">Platform-dependent efficient copy operations</a></li>110<li><a class="reference internal" href="#copytree-example">copytree example</a></li>111<li><a class="reference internal" href="#rmtree-example">rmtree example</a></li>112</ul>113</li>114<li><a class="reference internal" href="#archiving-operations">Archiving operations</a><ul>115<li><a class="reference internal" href="#archiving-example">Archiving example</a></li>116<li><a class="reference internal" href="#archiving-example-with-base-dir">Archiving example with <em>base_dir</em></a></li>117</ul>118</li>119<li><a class="reference internal" href="#querying-the-size-of-the-output-terminal">Querying the size of the output terminal</a></li>120</ul>121</li>122</ul>123 124  </div>125  <div>126    <h4>Previous topic</h4>127    <p class="topless"><a href="linecache.html"128                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">linecache</span></code> — Random access to text lines</a></p>129  </div>130  <div>131    <h4>Next topic</h4>132    <p class="topless"><a href="persistence.html"133                          title="next chapter">Data Persistence</a></p>134  </div>135  <script>136    document.addEventListener('DOMContentLoaded', () => {137        const title = document.querySelector('meta[property="og:title"]').content;138        const elements = document.querySelectorAll('.improvepage');139        const pageurl = window.location.href.split('?')[0];140        elements.forEach(element => {141            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));142            url.searchParams.set('pagetitle', title);143            url.searchParams.set('pageurl', pageurl);144            url.searchParams.set('pagesource', "library/shutil.rst");145            element.href = url.toString();146        });147    });148  </script>149  <div role="note" aria-label="source link">150    <h3>This page</h3>151    <ul class="this-page-menu">152      <li><a href="../bugs.html">Report a bug</a></li>153      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>154      <li>155        <a href="https://github.com/python/cpython/blob/main/Doc/library/shutil.rst?plain=1"156            rel="nofollow">Show source157        </a>158      </li>159      160    </ul>161  </div>162        </nav>163    </div>164</div>165 166  167    <div class="related" role="navigation" aria-label="Related">168      <h3>Navigation</h3>169      <ul>170        <li class="right" style="margin-right: 10px">171          <a href="../genindex.html" title="General Index"172             accesskey="I">index</a></li>173        <li class="right" >174          <a href="../py-modindex.html" title="Python Module Index"175             >modules</a> |</li>176        <li class="right" >177          <a href="persistence.html" title="Data Persistence"178             accesskey="N">next</a> |</li>179        <li class="right" >180          <a href="linecache.html" title="linecache — Random access to text lines"181             accesskey="P">previous</a> |</li>182 183          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>184          <li><a href="https://www.python.org/">Python</a> &#187;</li>185          <li class="switchers">186            <div class="language_switcher_placeholder"></div>187            <div class="version_switcher_placeholder"></div>188          </li>189          <li>190              191          </li>192    <li id="cpython-language-and-version">193      <a href="../index.html">3.15.0a6 Documentation</a> &#187;194    </li>195 196          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>197          <li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">File and Directory Access</a> &#187;</li>198        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> — High-level file operations</a></li>199                <li class="right">200                    201 202    <div class="inline-search" role="search">203        <form class="inline-search" action="../search.html" method="get">204          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">205          <input type="submit" value="Go">206        </form>207    </div>208                     |209                </li>210            <li class="right">211<label class="theme-selector-label">212    Theme213    <select class="theme-selector" oninput="activateTheme(this.value)">214        <option value="auto" selected>Auto</option>215        <option value="light">Light</option>216        <option value="dark">Dark</option>217    </select>218</label> |</li>219            220      </ul>221    </div>    222 223    <div class="document">224      <div class="documentwrapper">225        <div class="bodywrapper">226          <div class="body" role="main">227            228  <section id="module-shutil">229<span id="shutil-high-level-file-operations"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> — High-level file operations<a class="headerlink" href="#module-shutil" title="Link to this heading">¶</a></h1>230<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/shutil.py">Lib/shutil.py</a></p>231<hr class="docutils" id="index-0" />232<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> module offers a number of high-level operations on files and233collections of files.  In particular, functions are provided  which support file234copying and removal. For operations on individual files, see also the235<a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module.</p>236<div class="admonition warning">237<p class="admonition-title">Warning</p>238<p>Even the higher-level file copying functions (<a class="reference internal" href="#shutil.copy" title="shutil.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copy()</span></code></a>,239<a class="reference internal" href="#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copy2()</span></code></a>) cannot copy all file metadata.</p>240<p>On POSIX platforms, this means that file owner and group are lost as well241as ACLs.  On Mac OS, the resource fork and other metadata are not used.242This means that resources will be lost and file type and creator codes will243not be correct. On Windows, file owners, ACLs and alternate data streams244are not copied.</p>245</div>246<section id="directory-and-files-operations">247<span id="file-operations"></span><h2>Directory and files operations<a class="headerlink" href="#directory-and-files-operations" title="Link to this heading">¶</a></h2>248<dl class="py function">249<dt class="sig sig-object py" id="shutil.copyfileobj">250<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">copyfileobj</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fsrc</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fdst</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">length</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.copyfileobj" title="Link to this definition">¶</a></dt>251<dd><p>Copy the contents of the <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file-like object</span></a> <em>fsrc</em> to the file-like object <em>fdst</em>.252The integer <em>length</em>, if given, is the buffer size. In particular, a negative253<em>length</em> value means to copy the data without looping over the source data in254chunks; by default the data is read in chunks to avoid uncontrolled memory255consumption. Note that if the current file position of the <em>fsrc</em> object is not2560, only the contents from the current file position to the end of the file will257be copied.</p>258<p><code class="xref py py-func docutils literal notranslate"><span class="pre">copyfileobj()</span></code> will <em>not</em> guarantee that the destination stream has259been flushed on completion of the copy. If you want to read from the260destination at the completion of the copy operation (for example, reading261the contents of a temporary file that has been copied from a HTTP stream),262you must ensure that you have called <a class="reference internal" href="io.html#io.IOBase.flush" title="io.IOBase.flush"><code class="xref py py-func docutils literal notranslate"><span class="pre">flush()</span></code></a> or263<a class="reference internal" href="io.html#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-func docutils literal notranslate"><span class="pre">close()</span></code></a> on the file-like object before attempting to read264the destination file.</p>265</dd></dl>266 267<dl class="py function">268<dt class="sig sig-object py" id="shutil.copyfile">269<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">copyfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</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="#shutil.copyfile" title="Link to this definition">¶</a></dt>270<dd><p>Copy the contents (no metadata) of the file named <em>src</em> to a file named271<em>dst</em> and return <em>dst</em> in the most efficient way possible.272<em>src</em> and <em>dst</em> are <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like objects</span></a> or path names given as strings.</p>273<p><em>dst</em> must be the complete target file name; look at <a class="reference internal" href="#shutil.copy" title="shutil.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy()</span></code></a>274for a copy that accepts a target directory path.  If <em>src</em> and <em>dst</em>275specify the same file, <a class="reference internal" href="#shutil.SameFileError" title="shutil.SameFileError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SameFileError</span></code></a> is raised.</p>276<p>The destination location must be writable; otherwise, an <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>277exception will be raised. If <em>dst</em> already exists, it will be replaced.278Special files such as character or block devices and pipes cannot be279copied with this function.</p>280<p>If <em>follow_symlinks</em> is false and <em>src</em> is a symbolic link,281a new symbolic link will be created instead of copying the282file <em>src</em> points to.</p>283<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copyfile</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>.</p>284<div class="versionchanged">285<p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> used to be raised instead of <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>.286Added <em>follow_symlinks</em> argument.287Now returns <em>dst</em>.</p>288</div>289<div class="versionchanged">290<p><span class="versionmodified changed">Changed in version 3.4: </span>Raise <a class="reference internal" href="#shutil.SameFileError" title="shutil.SameFileError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SameFileError</span></code></a> instead of <a class="reference internal" href="#shutil.Error" title="shutil.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a>.  Since the former is291a subclass of the latter, this change is backward compatible.</p>292</div>293<div class="versionchanged">294<p><span class="versionmodified changed">Changed in version 3.8: </span>Platform-specific fast-copy syscalls may be used internally in order to295copy the file more efficiently. See296<a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">Platform-dependent efficient copy operations</span></a> section.</p>297</div>298</dd></dl>299 300<dl class="py exception">301<dt class="sig sig-object py" id="shutil.SpecialFileError">302<em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">SpecialFileError</span></span><a class="headerlink" href="#shutil.SpecialFileError" title="Link to this definition">¶</a></dt>303<dd><p>This exception is raised when <a class="reference internal" href="#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">copyfile()</span></code></a> or <a class="reference internal" href="#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">copytree()</span></code></a> attempt304to copy a named pipe.</p>305<div class="versionadded">306<p><span class="versionmodified added">Added in version 2.7.</span></p>307</div>308</dd></dl>309 310<dl class="py exception">311<dt class="sig sig-object py" id="shutil.SameFileError">312<em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">SameFileError</span></span><a class="headerlink" href="#shutil.SameFileError" title="Link to this definition">¶</a></dt>313<dd><p>This exception is raised if source and destination in <a class="reference internal" href="#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">copyfile()</span></code></a>314are the same file.</p>315<div class="versionadded">316<p><span class="versionmodified added">Added in version 3.4.</span></p>317</div>318</dd></dl>319 320<dl class="py function">321<dt class="sig sig-object py" id="shutil.copymode">322<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">copymode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</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="#shutil.copymode" title="Link to this definition">¶</a></dt>323<dd><p>Copy the permission bits from <em>src</em> to <em>dst</em>.  The file contents, owner, and324group are unaffected.  <em>src</em> and <em>dst</em> are <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like objects</span></a> or path names325given as strings.326If <em>follow_symlinks</em> is false, and both <em>src</em> and <em>dst</em> are symbolic links,327<code class="xref py py-func docutils literal notranslate"><span class="pre">copymode()</span></code> will attempt to modify the mode of <em>dst</em> itself (rather328than the file it points to).  This functionality is not available on every329platform; please see <a class="reference internal" href="#shutil.copystat" title="shutil.copystat"><code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code></a> for more information.  If330<code class="xref py py-func docutils literal notranslate"><span class="pre">copymode()</span></code> cannot modify symbolic links on the local platform, and it331is asked to do so, it will do nothing and return.</p>332<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copymode</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>.</p>333<div class="versionchanged">334<p><span class="versionmodified changed">Changed in version 3.3: </span>Added <em>follow_symlinks</em> argument.</p>335</div>336</dd></dl>337 338<dl class="py function">339<dt class="sig sig-object py" id="shutil.copystat">340<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">copystat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</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="#shutil.copystat" title="Link to this definition">¶</a></dt>341<dd><p>Copy the permission bits, last access time, last modification time, and342flags from <em>src</em> to <em>dst</em>.  On Linux, <code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code> also copies the343“extended attributes” where possible.  The file contents, owner, and344group are unaffected.  <em>src</em> and <em>dst</em> are <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like objects</span></a> or path345names given as strings.</p>346<p>If <em>follow_symlinks</em> is false, and <em>src</em> and <em>dst</em> both347refer to symbolic links, <code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code> will operate on348the symbolic links themselves rather than the files the349symbolic links refer to—reading the information from the350<em>src</em> symbolic link, and writing the information to the351<em>dst</em> symbolic link.</p>352<div class="admonition note">353<p class="admonition-title">Note</p>354<p>Not all platforms provide the ability to examine and355modify symbolic links.  Python itself can tell you what356functionality is locally available.</p>357<ul class="simple">358<li><p>If <code class="docutils literal notranslate"><span class="pre">os.chmod</span> <span class="pre">in</span> <span class="pre">os.supports_follow_symlinks</span></code> is359<code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code> can modify the permission360bits of a symbolic link.</p></li>361<li><p>If <code class="docutils literal notranslate"><span class="pre">os.utime</span> <span class="pre">in</span> <span class="pre">os.supports_follow_symlinks</span></code> is362<code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code> can modify the last access363and modification times of a symbolic link.</p></li>364<li><p>If <code class="docutils literal notranslate"><span class="pre">os.chflags</span> <span class="pre">in</span> <span class="pre">os.supports_follow_symlinks</span></code> is365<code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code> can modify the flags of366a symbolic link.  (<code class="docutils literal notranslate"><span class="pre">os.chflags</span></code> is not available on367all platforms.)</p></li>368</ul>369<p>On platforms where some or all of this functionality370is unavailable, when asked to modify a symbolic link,371<code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code> will copy everything it can.372<code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code> never returns failure.</p>373<p>Please see <a class="reference internal" href="os.html#os.supports_follow_symlinks" title="os.supports_follow_symlinks"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.supports_follow_symlinks</span></code></a>374for more information.</p>375</div>376<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copystat</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>.</p>377<div class="versionchanged">378<p><span class="versionmodified changed">Changed in version 3.3: </span>Added <em>follow_symlinks</em> argument and support for Linux extended attributes.</p>379</div>380</dd></dl>381 382<dl class="py function">383<dt class="sig sig-object py" id="shutil.copy">384<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">copy</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</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="#shutil.copy" title="Link to this definition">¶</a></dt>385<dd><p>Copies the file <em>src</em> to the file or directory <em>dst</em>.  <em>src</em> and <em>dst</em>386should be <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like objects</span></a> or strings.  If387<em>dst</em> specifies a directory, the file will be copied into <em>dst</em> using the388base filename from <em>src</em>. If <em>dst</em> specifies a file that already exists,389it will be replaced. Returns the path to the newly created file.</p>390<p>If <em>follow_symlinks</em> is false, and <em>src</em> is a symbolic link,391<em>dst</em> will be created as a symbolic link.  If <em>follow_symlinks</em>392is true and <em>src</em> is a symbolic link, <em>dst</em> will be a copy of393the file <em>src</em> refers to.</p>394<p><a class="reference internal" href="#shutil.copy" title="shutil.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy()</span></code></a> copies the file data and the file’s permission395mode (see <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>).  Other metadata, like the396file’s creation and modification times, is not preserved.397To preserve all file metadata from the original, use398<a class="reference internal" href="#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code></a> instead.</p>399<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copyfile</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>.</p>400<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copymode</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>.</p>401<div class="versionchanged">402<p><span class="versionmodified changed">Changed in version 3.3: </span>Added <em>follow_symlinks</em> argument.403Now returns path to the newly created file.</p>404</div>405<div class="versionchanged">406<p><span class="versionmodified changed">Changed in version 3.8: </span>Platform-specific fast-copy syscalls may be used internally in order to407copy the file more efficiently. See408<a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">Platform-dependent efficient copy operations</span></a> section.</p>409</div>410</dd></dl>411 412<dl class="py function">413<dt class="sig sig-object py" id="shutil.copy2">414<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">copy2</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</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="#shutil.copy2" title="Link to this definition">¶</a></dt>415<dd><p>Identical to <a class="reference internal" href="#shutil.copy" title="shutil.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy()</span></code></a> except that <code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code>416also attempts to preserve file metadata.</p>417<p>When <em>follow_symlinks</em> is false, and <em>src</em> is a symbolic418link, <code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code> attempts to copy all metadata from the419<em>src</em> symbolic link to the newly created <em>dst</em> symbolic link.420However, this functionality is not available on all platforms.421On platforms where some or all of this functionality is422unavailable, <code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code> will preserve all the metadata423it can; <code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code> never raises an exception because it424cannot preserve file metadata.</p>425<p><code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code> uses <a class="reference internal" href="#shutil.copystat" title="shutil.copystat"><code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code></a> to copy the file metadata.426Please see <code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code> for more information427about platform support for modifying symbolic link metadata.</p>428<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copyfile</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>.</p>429<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copystat</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>.</p>430<div class="versionchanged">431<p><span class="versionmodified changed">Changed in version 3.3: </span>Added <em>follow_symlinks</em> argument, try to copy extended432file system attributes too (currently Linux only).433Now returns path to the newly created file.</p>434</div>435<div class="versionchanged">436<p><span class="versionmodified changed">Changed in version 3.8: </span>Platform-specific fast-copy syscalls may be used internally in order to437copy the file more efficiently. See438<a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">Platform-dependent efficient copy operations</span></a> section.</p>439</div>440</dd></dl>441 442<dl class="py function">443<dt class="sig sig-object py" id="shutil.ignore_patterns">444<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">ignore_patterns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">patterns</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.ignore_patterns" title="Link to this definition">¶</a></dt>445<dd><p>This factory function creates a function that can be used as a callable for446<a class="reference internal" href="#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">copytree()</span></code></a>'s <em>ignore</em> argument, ignoring files and directories that447match one of the glob-style <em>patterns</em> provided.  See the example below.</p>448</dd></dl>449 450<dl class="py function">451<dt class="sig sig-object py" id="shutil.copytree">452<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">copytree</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symlinks</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</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">copy_function</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">copy2</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignore_dangling_symlinks</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">dirs_exist_ok</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.copytree" title="Link to this definition">¶</a></dt>453<dd><p>Recursively copy an entire directory tree rooted at <em>src</em> to a directory454named <em>dst</em> and return the destination directory.  All intermediate455directories needed to contain <em>dst</em> will also be created by default.</p>456<p>Permissions and times of directories are copied with <a class="reference internal" href="#shutil.copystat" title="shutil.copystat"><code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code></a>,457individual files are copied using <a class="reference internal" href="#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code></a>.</p>458<p>If <em>symlinks</em> is true, symbolic links in the source tree are represented as459symbolic links in the new tree and the metadata of the original links will460be copied as far as the platform allows; if false or omitted, the contents461and metadata of the linked files are copied to the new tree.</p>462<p>When <em>symlinks</em> is false, if the file pointed to by the symlink doesn’t463exist, an exception will be added in the list of errors raised in464an <a class="reference internal" href="#shutil.Error" title="shutil.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a> exception at the end of the copy process.465You can set the optional <em>ignore_dangling_symlinks</em> flag to true if you466want to silence this exception. Notice that this option has no effect467on platforms that don’t support <a class="reference internal" href="os.html#os.symlink" title="os.symlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.symlink()</span></code></a>.</p>468<p>If <em>ignore</em> is given, it must be a callable that will receive as its469arguments the directory being visited by <code class="xref py py-func docutils literal notranslate"><span class="pre">copytree()</span></code>, and a list of its470contents, as returned by <a class="reference internal" href="os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a>.  Since <code class="xref py py-func docutils literal notranslate"><span class="pre">copytree()</span></code> is471called recursively, the <em>ignore</em> callable will be called once for each472directory that is copied.  The callable must return a sequence of directory473and file names relative to the current directory (i.e. a subset of the items474in its second argument); these names will then be ignored in the copy475process.  <a class="reference internal" href="#shutil.ignore_patterns" title="shutil.ignore_patterns"><code class="xref py py-func docutils literal notranslate"><span class="pre">ignore_patterns()</span></code></a> can be used to create such a callable that476ignores names based on glob-style patterns.</p>477<p>If exception(s) occur, an <a class="reference internal" href="#shutil.Error" title="shutil.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a> is raised with a list of reasons.</p>478<p>If <em>copy_function</em> is given, it must be a callable that will be used to copy479each file. It will be called with the source path and the destination path480as arguments. By default, <a class="reference internal" href="#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code></a> is used, but any function481that supports the same signature (like <a class="reference internal" href="#shutil.copy" title="shutil.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy()</span></code></a>) can be used.</p>482<p>If <em>dirs_exist_ok</em> is false (the default) and <em>dst</em> already exists, a483<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> is raised. If <em>dirs_exist_ok</em> is true, the copying484operation will continue if it encounters existing directories, and files485within the <em>dst</em> tree will be overwritten by corresponding files from the486<em>src</em> tree.</p>487<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copytree</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>.</p>488<div class="versionchanged">489<p><span class="versionmodified changed">Changed in version 3.2: </span>Added the <em>copy_function</em> argument to be able to provide a custom copy490function.491Added the <em>ignore_dangling_symlinks</em> argument to silence dangling symlinks492errors when <em>symlinks</em> is false.</p>493</div>494<div class="versionchanged">495<p><span class="versionmodified changed">Changed in version 3.3: </span>Copy metadata when <em>symlinks</em> is false.496Now returns <em>dst</em>.</p>497</div>498<div class="versionchanged">499<p><span class="versionmodified changed">Changed in version 3.8: </span>Platform-specific fast-copy syscalls may be used internally in order to500copy the file more efficiently. See501<a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">Platform-dependent efficient copy operations</span></a> section.</p>502</div>503<div class="versionchanged">504<p><span class="versionmodified changed">Changed in version 3.8: </span>Added the <em>dirs_exist_ok</em> parameter.</p>505</div>506</dd></dl>507 508<dl class="py function">509<dt class="sig sig-object py" id="shutil.rmtree">510<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">rmtree</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignore_errors</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">onerror</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">onexc</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">dir_fd</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="#shutil.rmtree" title="Link to this definition">¶</a></dt>511<dd><p id="index-1">Delete an entire directory tree; <em>path</em> must point to a directory (but not a512symbolic link to a directory).  If <em>ignore_errors</em> is true, errors resulting513from failed removals will be ignored; if false or omitted, such errors are514handled by calling a handler specified by <em>onexc</em> or <em>onerror</em> or, if both515are omitted, exceptions are propagated to the caller.</p>516<p>This function can support <a class="reference internal" href="os.html#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p>517<div class="admonition note">518<p class="admonition-title">Note</p>519<p>On platforms that support the necessary fd-based functions a symlink520attack resistant version of <code class="xref py py-func docutils literal notranslate"><span class="pre">rmtree()</span></code> is used by default.  On other521platforms, the <code class="xref py py-func docutils literal notranslate"><span class="pre">rmtree()</span></code> implementation is susceptible to a symlink522attack: given proper timing and circumstances, attackers can manipulate523symlinks on the filesystem to delete files they wouldn’t be able to access524otherwise.  Applications can use the <a class="reference internal" href="#shutil.rmtree.avoids_symlink_attacks" title="shutil.rmtree.avoids_symlink_attacks"><code class="xref py py-data docutils literal notranslate"><span class="pre">rmtree.avoids_symlink_attacks</span></code></a>525function attribute to determine which case applies.</p>526</div>527<p>If <em>onexc</em> is provided, it must be a callable that accepts three parameters:528<em>function</em>, <em>path</em>, and <em>excinfo</em>.</p>529<p>The first parameter, <em>function</em>, is the function which raised the exception;530it depends on the platform and implementation.  The second parameter,531<em>path</em>, will be the path name passed to <em>function</em>.  The third parameter,532<em>excinfo</em>, is the exception that was raised. Exceptions raised by <em>onexc</em>533will not be caught.</p>534<p>The deprecated <em>onerror</em> is similar to <em>onexc</em>, except that the third535parameter it receives is the tuple returned from <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>.</p>536<div class="admonition seealso">537<p class="admonition-title">See also</p>538<p><a class="reference internal" href="#shutil-rmtree-example"><span class="std std-ref">rmtree example</span></a> for an example of handling the removal539of a directory tree that contains read-only files.</p>540</div>541<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.rmtree</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>542<div class="versionchanged">543<p><span class="versionmodified changed">Changed in version 3.3: </span>Added a symlink attack resistant version that is used automatically544if platform supports fd-based functions.</p>545</div>546<div class="versionchanged">547<p><span class="versionmodified changed">Changed in version 3.8: </span>On Windows, will no longer delete the contents of a directory junction548before removing the junction.</p>549</div>550<div class="versionchanged">551<p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>dir_fd</em> parameter.</p>552</div>553<div class="versionchanged">554<p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>onexc</em> parameter, deprecated <em>onerror</em>.</p>555</div>556<div class="versionchanged">557<p><span class="versionmodified changed">Changed in version 3.13: </span><code class="xref py py-func docutils literal notranslate"><span class="pre">rmtree()</span></code> now ignores <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> exceptions for all558but the top-level path.559Exceptions other than <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> and subclasses of <code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code>560are now always propagated to the caller.</p>561</div>562<dl class="py attribute">563<dt class="sig sig-object py" id="shutil.rmtree.avoids_symlink_attacks">564<span class="sig-prename descclassname"><span class="pre">rmtree.</span></span><span class="sig-name descname"><span class="pre">avoids_symlink_attacks</span></span><a class="headerlink" href="#shutil.rmtree.avoids_symlink_attacks" title="Link to this definition">¶</a></dt>565<dd><p>Indicates whether the current platform and implementation provides a566symlink attack resistant version of <code class="xref py py-func docutils literal notranslate"><span class="pre">rmtree()</span></code>.  Currently this is567only true for platforms supporting fd-based directory access functions.</p>568<div class="versionadded">569<p><span class="versionmodified added">Added in version 3.3.</span></p>570</div>571</dd></dl>572 573</dd></dl>574 575<dl class="py function">576<dt class="sig sig-object py" id="shutil.move">577<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">move</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dst</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">copy_function</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">copy2</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.move" title="Link to this definition">¶</a></dt>578<dd><p>Recursively move a file or directory (<em>src</em>) to another location and return579the destination.</p>580<p>If <em>dst</em> is an existing directory or a symlink to a directory, then <em>src</em>581is moved inside that directory. The destination path in that directory must582not already exist.</p>583<p>If <em>dst</em> already exists but is not a directory, it may be overwritten584depending on <a class="reference internal" href="os.html#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rename()</span></code></a> semantics.</p>585<p>If the destination is on the current filesystem, then <a class="reference internal" href="os.html#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rename()</span></code></a> is586used. Otherwise, <em>src</em> is copied to the destination using <em>copy_function</em>587and then removed.  In case of symlinks, a new symlink pointing to the target588of <em>src</em> will be created as the destination and <em>src</em> will be removed.</p>589<p>If <em>copy_function</em> is given, it must be a callable that takes two arguments,590<em>src</em> and the destination, and will be used to copy <em>src</em> to the destination591if <a class="reference internal" href="os.html#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rename()</span></code></a> cannot be used.  If the source is a directory,592<a class="reference internal" href="#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">copytree()</span></code></a> is called, passing it the <em>copy_function</em>. The593default <em>copy_function</em> is <a class="reference internal" href="#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code></a>.  Using <a class="reference internal" href="#shutil.copy" title="shutil.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy()</span></code></a> as the594<em>copy_function</em> allows the move to succeed when it is not possible to also595copy the metadata, at the expense of not copying any of the metadata.</p>596<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.move</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>.</p>597<div class="versionchanged">598<p><span class="versionmodified changed">Changed in version 3.3: </span>Added explicit symlink handling for foreign filesystems, thus adapting599it to the behavior of GNU’s <strong class="program">mv</strong>.600Now returns <em>dst</em>.</p>601</div>602<div class="versionchanged">603<p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>copy_function</em> keyword argument.</p>604</div>605<div class="versionchanged">606<p><span class="versionmodified changed">Changed in version 3.8: </span>Platform-specific fast-copy syscalls may be used internally in order to607copy the file more efficiently. See608<a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">Platform-dependent efficient copy operations</span></a> section.</p>609</div>610<div class="versionchanged">611<p><span class="versionmodified changed">Changed in version 3.9: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for both <em>src</em> and <em>dst</em>.</p>612</div>613</dd></dl>614 615<dl class="py function">616<dt class="sig sig-object py" id="shutil.disk_usage">617<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">disk_usage</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.disk_usage" title="Link to this definition">¶</a></dt>618<dd><p>Return disk usage statistics about the given path as a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a>619with the attributes <em>total</em>, <em>used</em> and <em>free</em>, which are the amount of620total, used and free space, in bytes. <em>path</em> may be a file or a621directory.</p>622<div class="admonition note">623<p class="admonition-title">Note</p>624<p>On Unix filesystems, <em>path</em> must point to a path within a <strong>mounted</strong>625filesystem partition. On those platforms, CPython doesn’t attempt to626retrieve disk usage information from non-mounted filesystems.</p>627</div>628<div class="versionadded">629<p><span class="versionmodified added">Added in version 3.3.</span></p>630</div>631<div class="versionchanged">632<p><span class="versionmodified changed">Changed in version 3.8: </span>On Windows, <em>path</em> can now be a file or directory.</p>633</div>634<div class="availability docutils container">635<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p>636</div>637</dd></dl>638 639<dl class="py function">640<dt class="sig sig-object py" id="shutil.chown">641<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">chown</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">user</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">group</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</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">follow_symlinks</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="#shutil.chown" title="Link to this definition">¶</a></dt>642<dd><p>Change owner <em>user</em> and/or <em>group</em> of the given <em>path</em>.</p>643<p><em>user</em> can be a system user name or a uid; the same applies to <em>group</em>. At644least one argument is required.</p>645<p>See also <a class="reference internal" href="os.html#os.chown" title="os.chown"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.chown()</span></code></a>, the underlying function.</p>646<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.chown</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">path</span></code>, <code class="docutils literal notranslate"><span class="pre">user</span></code>, <code class="docutils literal notranslate"><span class="pre">group</span></code>.</p>647<div class="availability docutils container">648<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>649</div>650<div class="versionadded">651<p><span class="versionmodified added">Added in version 3.3.</span></p>652</div>653<div class="versionchanged">654<p><span class="versionmodified changed">Changed in version 3.13: </span>Added <em>dir_fd</em> and <em>follow_symlinks</em> parameters.</p>655</div>656</dd></dl>657 658<dl class="py function">659<dt class="sig sig-object py" id="shutil.which">660<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">which</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmd</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">os.F_OK</span> <span class="pre">|</span> <span class="pre">os.X_OK</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.which" title="Link to this definition">¶</a></dt>661<dd><p>Return the path to an executable which would be run if the given <em>cmd</em> was662called.  If no <em>cmd</em> would be called, return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>663<p><em>mode</em> is a permission mask passed to <a class="reference internal" href="os.html#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.access()</span></code></a>, by default664determining if the file exists and is executable.</p>665<p><em>path</em> is a “<code class="docutils literal notranslate"><span class="pre">PATH</span></code> string” specifying the directories to look in,666delimited by <a class="reference internal" href="os.html#os.pathsep" title="os.pathsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.pathsep</span></code></a>. When no <em>path</em> is specified, the667<span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> environment variable is read from <a class="reference internal" href="os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>,668falling back to <a class="reference internal" href="os.html#os.defpath" title="os.defpath"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.defpath</span></code></a> if it is not set.</p>669<p>If <em>cmd</em> contains a directory component, <code class="xref py py-func docutils literal notranslate"><span class="pre">which()</span></code> only checks the670specified path directly and does not search the directories listed in671<em>path</em> or in the system’s <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> environment variable.</p>672<p>On Windows, the current directory is prepended to the <em>path</em> if <em>mode</em> does673not include <code class="docutils literal notranslate"><span class="pre">os.X_OK</span></code>. When the <em>mode</em> does include <code class="docutils literal notranslate"><span class="pre">os.X_OK</span></code>, the674Windows API <code class="docutils literal notranslate"><span class="pre">NeedCurrentDirectoryForExePathW</span></code> will be consulted to675determine if the current directory should be prepended to <em>path</em>. To avoid676consulting the current working directory for executables: set the environment677variable <code class="docutils literal notranslate"><span class="pre">NoDefaultCurrentDirectoryInExePath</span></code>.</p>678<p>Also on Windows, the <span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATHEXT</span></code> environment variable is used to679resolve commands that may not already include an extension. For example,680if you call <code class="docutils literal notranslate"><span class="pre">shutil.which(&quot;python&quot;)</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">which()</span></code> will search <code class="docutils literal notranslate"><span class="pre">PATHEXT</span></code>681to know that it should look for <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> within the <em>path</em>682directories. For example, on Windows:</p>683<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">shutil</span><span class="o">.</span><span class="n">which</span><span class="p">(</span><span class="s2">&quot;python&quot;</span><span class="p">)</span>684<span class="go">&#39;C:\\Python33\\python.EXE&#39;</span>685</pre></div>686</div>687<p>This is also applied when <em>cmd</em> is a path that contains a directory688component:</p>689<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">shutil</span><span class="o">.</span><span class="n">which</span><span class="p">(</span><span class="s2">&quot;C:</span><span class="se">\\</span><span class="s2">Python33</span><span class="se">\\</span><span class="s2">python&quot;</span><span class="p">)</span>690<span class="go">&#39;C:\\Python33\\python.EXE&#39;</span>691</pre></div>692</div>693<div class="versionadded">694<p><span class="versionmodified added">Added in version 3.3.</span></p>695</div>696<div class="versionchanged">697<p><span class="versionmodified changed">Changed in version 3.8: </span>The <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> type is now accepted.  If <em>cmd</em> type is698<code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code>, the result type is also <code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code>.</p>699</div>700<div class="versionchanged">701<p><span class="versionmodified changed">Changed in version 3.12: </span>On Windows, the current directory is no longer prepended to the search702path if <em>mode</em> includes <code class="docutils literal notranslate"><span class="pre">os.X_OK</span></code> and WinAPI703<code class="docutils literal notranslate"><span class="pre">NeedCurrentDirectoryForExePathW(cmd)</span></code> is false, else the current704directory is prepended even if it is already in the search path;705<code class="docutils literal notranslate"><span class="pre">PATHEXT</span></code> is used now even when <em>cmd</em> includes a directory component706or ends with an extension that is in <code class="docutils literal notranslate"><span class="pre">PATHEXT</span></code>; and filenames that707have no extension can now be found.</p>708</div>709</dd></dl>710 711<dl class="py exception">712<dt class="sig sig-object py" id="shutil.Error">713<em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">Error</span></span><a class="headerlink" href="#shutil.Error" title="Link to this definition">¶</a></dt>714<dd><p>Subclass of <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> collecting exceptions raised during a multi-file715operation. For <a class="reference internal" href="#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">copytree()</span></code></a>, the exception argument is a list of 3-tuples716(<em>srcname</em>, <em>dstname</em>, <em>exception</em>).</p>717</dd></dl>718 719<section id="platform-dependent-efficient-copy-operations">720<span id="shutil-platform-dependent-efficient-copy-operations"></span><h3>Platform-dependent efficient copy operations<a class="headerlink" href="#platform-dependent-efficient-copy-operations" title="Link to this heading">¶</a></h3>721<p>Starting from Python 3.8, all functions involving a file copy722(<a class="reference internal" href="#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">copyfile()</span></code></a>, <a class="reference internal" href="#shutil.copy" title="shutil.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy()</span></code></a>, <a class="reference internal" href="#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy2()</span></code></a>,723<a class="reference internal" href="#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">copytree()</span></code></a>, and <a class="reference internal" href="#shutil.move" title="shutil.move"><code class="xref py py-func docutils literal notranslate"><span class="pre">move()</span></code></a>) may use724platform-specific “fast-copy” syscalls in order to copy the file more725efficiently (see <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=33671">bpo-33671</a>).726“fast-copy” means that the copying operation occurs within the kernel, avoiding727the use of userspace buffers in Python as in “<code class="docutils literal notranslate"><span class="pre">outfd.write(infd.read())</span></code>”.</p>728<p>On macOS <a class="reference external" href="http://www.manpagez.com/man/3/copyfile/">fcopyfile</a> is used to copy the file content (not metadata).</p>729<p>On Linux <a class="reference internal" href="os.html#os.copy_file_range" title="os.copy_file_range"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.copy_file_range()</span></code></a> or <a class="reference internal" href="os.html#os.sendfile" title="os.sendfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.sendfile()</span></code></a> is used.</p>730<p>On Solaris <a class="reference internal" href="os.html#os.sendfile" title="os.sendfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.sendfile()</span></code></a> is used.</p>731<p>On Windows <a class="reference internal" href="#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copyfile()</span></code></a> uses a bigger default buffer size (1 MiB732instead of 64 KiB) and a <a class="reference internal" href="stdtypes.html#memoryview" title="memoryview"><code class="xref py py-func docutils literal notranslate"><span class="pre">memoryview()</span></code></a>-based variant of733<a class="reference internal" href="#shutil.copyfileobj" title="shutil.copyfileobj"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copyfileobj()</span></code></a> is used, which still reads and writes in a loop.734<a class="reference internal" href="#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copy2()</span></code></a> uses the native <code class="docutils literal notranslate"><span class="pre">CopyFile2</span></code> call on Windows, which is the most735efficient method, supports copy-on-write, and preserves metadata.</p>736<p>If the fast-copy operation fails and no data was written in the destination737file then shutil will silently fall back to less efficient738<a class="reference internal" href="#shutil.copyfileobj" title="shutil.copyfileobj"><code class="xref py py-func docutils literal notranslate"><span class="pre">copyfileobj()</span></code></a> function internally.</p>739<div class="versionchanged">740<p><span class="versionmodified changed">Changed in version 3.8.</span></p>741</div>742<div class="versionchanged">743<p><span class="versionmodified changed">Changed in version 3.14: </span>Solaris now uses <a class="reference internal" href="os.html#os.sendfile" title="os.sendfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.sendfile()</span></code></a>.</p>744</div>745<div class="versionchanged">746<p><span class="versionmodified changed">Changed in version 3.14: </span>Copy-on-write or server-side copy may be used internally via747<a class="reference internal" href="os.html#os.copy_file_range" title="os.copy_file_range"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.copy_file_range()</span></code></a> on supported Linux filesystems.</p>748</div>749</section>750<section id="copytree-example">751<span id="shutil-copytree-example"></span><h3>copytree example<a class="headerlink" href="#copytree-example" title="Link to this heading">¶</a></h3>752<p>An example that uses the <a class="reference internal" href="#shutil.ignore_patterns" title="shutil.ignore_patterns"><code class="xref py py-func docutils literal notranslate"><span class="pre">ignore_patterns()</span></code></a> helper:</p>753<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">shutil</span><span class="w"> </span><span class="kn">import</span> <span class="n">copytree</span><span class="p">,</span> <span class="n">ignore_patterns</span>754 755<span class="n">copytree</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">destination</span><span class="p">,</span> <span class="n">ignore</span><span class="o">=</span><span class="n">ignore_patterns</span><span class="p">(</span><span class="s1">&#39;*.pyc&#39;</span><span class="p">,</span> <span class="s1">&#39;tmp*&#39;</span><span class="p">))</span>756</pre></div>757</div>758<p>This will copy everything except <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files and files or directories whose759name starts with <code class="docutils literal notranslate"><span class="pre">tmp</span></code>.</p>760<p>Another example that uses the <em>ignore</em> argument to add a logging call:</p>761<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">shutil</span><span class="w"> </span><span class="kn">import</span> <span class="n">copytree</span>762<span class="kn">import</span><span class="w"> </span><span class="nn">logging</span>763 764<span class="k">def</span><span class="w"> </span><span class="nf">_logpath</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">names</span><span class="p">):</span>765    <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Working in </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>766    <span class="k">return</span> <span class="p">[]</span>   <span class="c1"># nothing will be ignored</span>767 768<span class="n">copytree</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">destination</span><span class="p">,</span> <span class="n">ignore</span><span class="o">=</span><span class="n">_logpath</span><span class="p">)</span>769</pre></div>770</div>771</section>772<section id="rmtree-example">773<span id="shutil-rmtree-example"></span><h3>rmtree example<a class="headerlink" href="#rmtree-example" title="Link to this heading">¶</a></h3>774<p>This example shows how to remove a directory tree on Windows where some775of the files have their read-only bit set. It uses the onexc callback776to clear the readonly bit and reattempt the remove. Any subsequent failure777will propagate.</p>778<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">os</span><span class="o">,</span><span class="w"> </span><span class="nn">stat</span>779<span class="kn">import</span><span class="w"> </span><span class="nn">shutil</span>780 781<span class="k">def</span><span class="w"> </span><span class="nf">remove_readonly</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">_</span><span class="p">):</span>782    <span class="s2">&quot;Clear the readonly bit and reattempt the removal&quot;</span>783    <span class="n">os</span><span class="o">.</span><span class="n">chmod</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">stat</span><span class="o">.</span><span class="n">S_IWRITE</span><span class="p">)</span>784    <span class="n">func</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>785 786<span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="n">onexc</span><span class="o">=</span><span class="n">remove_readonly</span><span class="p">)</span>787</pre></div>788</div>789</section>790</section>791<section id="archiving-operations">792<span id="id1"></span><h2>Archiving operations<a class="headerlink" href="#archiving-operations" title="Link to this heading">¶</a></h2>793<div class="versionadded">794<p><span class="versionmodified added">Added in version 3.2.</span></p>795</div>796<div class="versionchanged">797<p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for the <em>xztar</em> format.</p>798</div>799<p>High-level utilities to create and read compressed and archived files are also800provided.  They rely on 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> and <a class="reference internal" href="tarfile.html#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> modules.</p>801<dl class="py function">802<dt class="sig sig-object py" id="shutil.make_archive">803<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">make_archive</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">base_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">format</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">root_dir</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">base_dir</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">dry_run</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">owner</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">group</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">logger</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.make_archive" title="Link to this definition">¶</a></dt>804<dd><p>Create an archive file (such as zip or tar) and return its name.</p>805<p><em>base_name</em> is a string or <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> specifying the name of806the file to create, including the path, minus any format-specific extension.</p>807<p><em>format</em> is the archive format: one of808“zip” (if the <a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> module is available), “tar”, “gztar” (if the809<code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code> module is available), “bztar” (if the <a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> module is810available), “xztar” (if the <a class="reference internal" href="lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> module is available), or “zstdtar”811(if the <a class="reference internal" href="compression.zstd.html#module-compression.zstd" title="compression.zstd: Low-level interface to compression and decompression routines in the zstd library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compression.zstd</span></code></a> module is available).</p>812<p><em>root_dir</em> is a string or <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> specifying a directory813that will be the root directory of the archive, all paths in the archive814will be relative to it; for example, we typically chdir into <em>root_dir</em>815before creating the archive.</p>816<p><em>base_dir</em> is a string or <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> specifying a directory817where we start archiving from; i.e. <em>base_dir</em> will be the common prefix of818all files and directories in the archive.  <em>base_dir</em> must be given relative819to <em>root_dir</em>.  See <a class="reference internal" href="#shutil-archiving-example-with-basedir"><span class="std std-ref">Archiving example with base_dir</span></a> for how to820use <em>base_dir</em> and <em>root_dir</em> together.</p>821<p><em>root_dir</em> and <em>base_dir</em> both default to the current directory.</p>822<p>If <em>dry_run</em> is true, no archive is created, but the operations that would be823executed are logged to <em>logger</em>.</p>824<p><em>owner</em> and <em>group</em> are used when creating a tar archive. By default,825uses the current owner and group.</p>826<p><em>logger</em> must be an object compatible with <span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0282/"><strong>PEP 282</strong></a>, usually an instance of827<a class="reference internal" href="logging.html#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.Logger</span></code></a>.</p>828<p>The <em>verbose</em> argument is unused and deprecated.</p>829<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.make_archive</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">base_name</span></code>, <code class="docutils literal notranslate"><span class="pre">format</span></code>, <code class="docutils literal notranslate"><span class="pre">root_dir</span></code>, <code class="docutils literal notranslate"><span class="pre">base_dir</span></code>.</p>830<div class="admonition note">831<p class="admonition-title">Note</p>832<p>This function is not thread-safe when custom archivers registered833with <a class="reference internal" href="#shutil.register_archive_format" title="shutil.register_archive_format"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_archive_format()</span></code></a> do not support the <em>root_dir</em>834argument.  In this case it835temporarily changes the current working directory of the process836to <em>root_dir</em> to perform archiving.</p>837</div>838<div class="versionchanged">839<p><span class="versionmodified changed">Changed in version 3.8: </span>The modern pax (POSIX.1-2001) format is now used instead of840the legacy GNU format for archives created with <code class="docutils literal notranslate"><span class="pre">format=&quot;tar&quot;</span></code>.</p>841</div>842<div class="versionchanged">843<p><span class="versionmodified changed">Changed in version 3.10.6: </span>This function is now made thread-safe during creation of standard844<code class="docutils literal notranslate"><span class="pre">.zip</span></code> and tar archives.</p>845</div>846<div class="versionchanged">847<p><span class="versionmodified changed">Changed in version 3.15.0a6 (unreleased): </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>base_name</em>, <em>root_dir</em> and848<em>base_dir</em>.</p>849</div>850</dd></dl>851 852<dl class="py function">853<dt class="sig sig-object py" id="shutil.get_archive_formats">854<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">get_archive_formats</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.get_archive_formats" title="Link to this definition">¶</a></dt>855<dd><p>Return a list of supported formats for archiving.856Each element of the returned sequence is a tuple <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">description)</span></code>.</p>857<p>By default <code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> provides these formats:</p>858<ul class="simple">859<li><p><em>zip</em>: ZIP file (if the <a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> module is available).</p></li>860<li><p><em>tar</em>: Uncompressed tar file. Uses POSIX.1-2001 pax format for new archives.</p></li>861<li><p><em>gztar</em>: gzip’ed tar-file (if the <a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> module is available).</p></li>862<li><p><em>bztar</em>: bzip2’ed tar-file (if the <a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> module is available).</p></li>863<li><p><em>xztar</em>: xz’ed tar-file (if the <a class="reference internal" href="lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> module is available).</p></li>864<li><p><em>zstdtar</em>: Zstandard compressed tar-file (if the <a class="reference internal" href="compression.zstd.html#module-compression.zstd" title="compression.zstd: Low-level interface to compression and decompression routines in the zstd library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compression.zstd</span></code></a>865module is available).</p></li>866</ul>867<p>You can register new formats or provide your own archiver for any existing868formats, by using <a class="reference internal" href="#shutil.register_archive_format" title="shutil.register_archive_format"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_archive_format()</span></code></a>.</p>869</dd></dl>870 871<dl class="py function">872<dt class="sig sig-object py" id="shutil.register_archive_format">873<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">register_archive_format</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">function</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">extra_args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">description</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.register_archive_format" title="Link to this definition">¶</a></dt>874<dd><p>Register an archiver for the format <em>name</em>.</p>875<p><em>function</em> is the callable that will be used to unpack archives. The callable876will receive the <em>base_name</em> of the file to create, followed by the877<em>base_dir</em> (which defaults to <a class="reference internal" href="os.html#os.curdir" title="os.curdir"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.curdir</span></code></a>) to start archiving from.878Further arguments are passed as keyword arguments: <em>owner</em>, <em>group</em>,879<em>dry_run</em> and <em>logger</em> (as passed in <a class="reference internal" href="#shutil.make_archive" title="shutil.make_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_archive()</span></code></a>).</p>880<p>If <em>function</em> has the custom attribute <code class="docutils literal notranslate"><span class="pre">function.supports_root_dir</span></code> set to <code class="docutils literal notranslate"><span class="pre">True</span></code>,881the <em>root_dir</em> argument is passed as a keyword argument.882Otherwise the current working directory of the process is temporarily883changed to <em>root_dir</em> before calling <em>function</em>.884In this case <a class="reference internal" href="#shutil.make_archive" title="shutil.make_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_archive()</span></code></a> is not thread-safe.</p>885<p>If given, <em>extra_args</em> is a sequence of <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> pairs that will be886used as extra keywords arguments when the archiver callable is used.</p>887<p><em>description</em> is used by <a class="reference internal" href="#shutil.get_archive_formats" title="shutil.get_archive_formats"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_archive_formats()</span></code></a> which returns the888list of archivers.  Defaults to an empty string.</p>889<div class="versionchanged">890<p><span class="versionmodified changed">Changed in version 3.12: </span>Added support for functions supporting the <em>root_dir</em> argument.</p>891</div>892</dd></dl>893 894<dl class="py function">895<dt class="sig sig-object py" id="shutil.unregister_archive_format">896<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">unregister_archive_format</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="#shutil.unregister_archive_format" title="Link to this definition">¶</a></dt>897<dd><p>Remove the archive format <em>name</em> from the list of supported formats.</p>898</dd></dl>899 900<dl class="py function">901<dt class="sig sig-object py" id="shutil.unpack_archive">902<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">unpack_archive</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">extract_dir</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">format</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">filter</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.unpack_archive" title="Link to this definition">¶</a></dt>903<dd><p>Unpack an archive. <em>filename</em> is the full path of the archive.</p>904<p><em>extract_dir</em> is the name of the target directory where the archive is905unpacked. If not provided, the current working directory is used.</p>906<p><em>format</em> is the archive format: one of “zip”, “tar”, “gztar”, “bztar”,907“xztar”, or “zstdtar”.  Or any other format registered with908<a class="reference internal" href="#shutil.register_unpack_format" title="shutil.register_unpack_format"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_unpack_format()</span></code></a>.  If not provided, <code class="xref py py-func docutils literal notranslate"><span class="pre">unpack_archive()</span></code>909will use the archive file name extension and see if an unpacker was910registered for that extension.  In case none is found,911a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>912<p>The keyword-only <em>filter</em> argument is passed to the underlying unpacking913function. For zip files, <em>filter</em> is not accepted.914For tar files, it is recommended to use <code class="docutils literal notranslate"><span class="pre">'data'</span></code> (default since Python9153.14), unless using features specific to tar and UNIX-like filesystems.916(See <a class="reference internal" href="tarfile.html#tarfile-extraction-filter"><span class="std std-ref">Extraction filters</span></a> for details.)</p>917<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.unpack_archive</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">filename</span></code>, <code class="docutils literal notranslate"><span class="pre">extract_dir</span></code>, <code class="docutils literal notranslate"><span class="pre">format</span></code>.</p>918<div class="admonition warning">919<p class="admonition-title">Warning</p>920<p>Never extract archives from untrusted sources without prior inspection.921It is possible that files are created outside of the path specified in922the <em>extract_dir</em> argument, e.g. members that have absolute filenames923starting with “/” or filenames with two dots “..”.</p>924<p>Since Python 3.14, the defaults for both built-in formats (zip and tar925files) will prevent the most dangerous of such security issues,926but will not prevent <em>all</em> unintended behavior.927Read the <a class="reference internal" href="tarfile.html#tarfile-further-verification"><span class="std std-ref">Hints for further verification</span></a>928section for tar-specific details.</p>929</div>930<div class="versionchanged">931<p><span class="versionmodified changed">Changed in version 3.7: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>filename</em> and <em>extract_dir</em>.</p>932</div>933<div class="versionchanged">934<p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>filter</em> argument.</p>935</div>936</dd></dl>937 938<dl class="py function">939<dt class="sig sig-object py" id="shutil.register_unpack_format">940<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">register_unpack_format</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">extensions</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">function</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">extra_args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">description</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.register_unpack_format" title="Link to this definition">¶</a></dt>941<dd><p>Registers an unpack format. <em>name</em> is the name of the format and942<em>extensions</em> is a list of extensions corresponding to the format, like943<code class="docutils literal notranslate"><span class="pre">.zip</span></code> for Zip files.</p>944<p><em>function</em> is the callable that will be used to unpack archives. The945callable will receive:</p>946<ul class="simple">947<li><p>the path of the archive, as a positional argument;</p></li>948<li><p>the directory the archive must be extracted to, as a positional argument;</p></li>949<li><p>possibly a <em>filter</em> keyword argument, if it was given to950<a class="reference internal" href="#shutil.unpack_archive" title="shutil.unpack_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">unpack_archive()</span></code></a>;</p></li>951<li><p>additional keyword arguments, specified by <em>extra_args</em> as a sequence952of <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> tuples.</p></li>953</ul>954<p><em>description</em> can be provided to describe the format, and will be returned955by the <a class="reference internal" href="#shutil.get_unpack_formats" title="shutil.get_unpack_formats"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_unpack_formats()</span></code></a> function.</p>956</dd></dl>957 958<dl class="py function">959<dt class="sig sig-object py" id="shutil.unregister_unpack_format">960<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">unregister_unpack_format</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="#shutil.unregister_unpack_format" title="Link to this definition">¶</a></dt>961<dd><p>Unregister an unpack format. <em>name</em> is the name of the format.</p>962</dd></dl>963 964<dl class="py function">965<dt class="sig sig-object py" id="shutil.get_unpack_formats">966<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">get_unpack_formats</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.get_unpack_formats" title="Link to this definition">¶</a></dt>967<dd><p>Return a list of all registered formats for unpacking.968Each element of the returned sequence is a tuple969<code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">extensions,</span> <span class="pre">description)</span></code>.</p>970<p>By default <code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> provides these formats:</p>971<ul class="simple">972<li><p><em>zip</em>: ZIP file (unpacking compressed files works only if the corresponding973module is available).</p></li>974<li><p><em>tar</em>: uncompressed tar file.</p></li>975<li><p><em>gztar</em>: gzip’ed tar-file (if the <a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> module is available).</p></li>976<li><p><em>bztar</em>: bzip2’ed tar-file (if the <a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> module is available).</p></li>977<li><p><em>xztar</em>: xz’ed tar-file (if the <a class="reference internal" href="lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> module is available).</p></li>978<li><p><em>zstdtar</em>: Zstandard compressed tar-file (if the <a class="reference internal" href="compression.zstd.html#module-compression.zstd" title="compression.zstd: Low-level interface to compression and decompression routines in the zstd library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compression.zstd</span></code></a>979module is available).</p></li>980</ul>981<p>You can register new formats or provide your own unpacker for any existing982formats, by using <a class="reference internal" href="#shutil.register_unpack_format" title="shutil.register_unpack_format"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_unpack_format()</span></code></a>.</p>983</dd></dl>984 985<section id="archiving-example">986<span id="shutil-archiving-example"></span><h3>Archiving example<a class="headerlink" href="#archiving-example" title="Link to this heading">¶</a></h3>987<p>In this example, we create a gzip’ed tar-file archive containing all files988found in the <code class="file docutils literal notranslate"><span class="pre">.ssh</span></code> directory of the user:</p>989<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span><span class="w"> </span><span class="nn">shutil</span><span class="w"> </span><span class="kn">import</span> <span class="n">make_archive</span>990<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">os</span>991<span class="gp">&gt;&gt;&gt; </span><span class="n">archive_name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;~&#39;</span><span class="p">,</span> <span class="s1">&#39;myarchive&#39;</span><span class="p">))</span>992<span class="gp">&gt;&gt;&gt; </span><span class="n">root_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;~&#39;</span><span class="p">,</span> <span class="s1">&#39;.ssh&#39;</span><span class="p">))</span>993<span class="gp">&gt;&gt;&gt; </span><span class="n">make_archive</span><span class="p">(</span><span class="n">archive_name</span><span class="p">,</span> <span class="s1">&#39;gztar&#39;</span><span class="p">,</span> <span class="n">root_dir</span><span class="p">)</span>994<span class="go">&#39;/Users/tarek/myarchive.tar.gz&#39;</span>995</pre></div>996</div>997<p>The resulting archive contains:</p>998<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>tar<span class="w"> </span>-tzvf<span class="w"> </span>/Users/tarek/myarchive.tar.gz999<span class="go">drwx------ tarek/staff       0 2010-02-01 16:23:40 ./</span>1000<span class="go">-rw-r--r-- tarek/staff     609 2008-06-09 13:26:54 ./authorized_keys</span>1001<span class="go">-rwxr-xr-x tarek/staff      65 2008-06-09 13:26:54 ./config</span>1002<span class="go">-rwx------ tarek/staff     668 2008-06-09 13:26:54 ./id_dsa</span>1003<span class="go">-rwxr-xr-x tarek/staff     609 2008-06-09 13:26:54 ./id_dsa.pub</span>1004<span class="go">-rw------- tarek/staff    1675 2008-06-09 13:26:54 ./id_rsa</span>1005<span class="go">-rw-r--r-- tarek/staff     397 2008-06-09 13:26:54 ./id_rsa.pub</span>1006<span class="go">-rw-r--r-- tarek/staff   37192 2010-02-06 18:23:10 ./known_hosts</span>1007</pre></div>1008</div>1009</section>1010<section id="archiving-example-with-base-dir">1011<span id="shutil-archiving-example-with-basedir"></span><h3>Archiving example with <em>base_dir</em><a class="headerlink" href="#archiving-example-with-base-dir" title="Link to this heading">¶</a></h3>1012<p>In this example, similar to the <a class="reference internal" href="#shutil-archiving-example">one above</a>,1013we show how to use <a class="reference internal" href="#shutil.make_archive" title="shutil.make_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_archive()</span></code></a>, but this time with the usage of1014<em>base_dir</em>.  We now have the following directory structure:</p>1015<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>tree<span class="w"> </span>tmp1016<span class="go">tmp</span>1017<span class="go">└── root</span>1018<span class="go">    └── structure</span>1019<span class="go">        ├── content</span>1020<span class="go">            └── please_add.txt</span>1021<span class="go">        └── do_not_add.txt</span>1022</pre></div>1023</div>1024<p>In the final archive, <code class="file docutils literal notranslate"><span class="pre">please_add.txt</span></code> should be included, but1025<code class="file docutils literal notranslate"><span class="pre">do_not_add.txt</span></code> should not.  Therefore we use the following:</p>1026<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span><span class="w"> </span><span class="nn">shutil</span><span class="w"> </span><span class="kn">import</span> <span class="n">make_archive</span>1027<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">os</span>1028<span class="gp">&gt;&gt;&gt; </span><span class="n">archive_name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;~&#39;</span><span class="p">,</span> <span class="s1">&#39;myarchive&#39;</span><span class="p">))</span>1029<span class="gp">&gt;&gt;&gt; </span><span class="n">make_archive</span><span class="p">(</span>1030<span class="gp">... </span>    <span class="n">archive_name</span><span class="p">,</span>1031<span class="gp">... </span>    <span class="s1">&#39;tar&#39;</span><span class="p">,</span>1032<span class="gp">... </span>    <span class="n">root_dir</span><span class="o">=</span><span class="s1">&#39;tmp/root&#39;</span><span class="p">,</span>1033<span class="gp">... </span>    <span class="n">base_dir</span><span class="o">=</span><span class="s1">&#39;structure/content&#39;</span><span class="p">,</span>1034<span class="gp">... </span><span class="p">)</span>1035<span class="go">&#39;/Users/tarek/myarchive.tar&#39;</span>1036</pre></div>1037</div>1038<p>Listing the files in the resulting archive gives us:</p>1039<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>tarfile<span class="w"> </span>-l<span class="w"> </span>/Users/tarek/myarchive.tar1040<span class="go">structure/content/</span>1041<span class="go">structure/content/please_add.txt</span>1042</pre></div>1043</div>1044</section>1045</section>1046<section id="querying-the-size-of-the-output-terminal">1047<h2>Querying the size of the output terminal<a class="headerlink" href="#querying-the-size-of-the-output-terminal" title="Link to this heading">¶</a></h2>1048<dl class="py function">1049<dt class="sig sig-object py" id="shutil.get_terminal_size">1050<span class="sig-prename descclassname"><span class="pre">shutil.</span></span><span class="sig-name descname"><span class="pre">get_terminal_size</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fallback</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">(columns,</span> <span class="pre">lines)</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.get_terminal_size" title="Link to this definition">¶</a></dt>1051<dd><p>Get the size of the terminal window.</p>1052<p>For each of the two dimensions, the environment variable, <code class="docutils literal notranslate"><span class="pre">COLUMNS</span></code>1053and <code class="docutils literal notranslate"><span class="pre">LINES</span></code> respectively, is checked. If the variable is defined and1054the value is a positive integer, it is used.</p>1055<p>When <code class="docutils literal notranslate"><span class="pre">COLUMNS</span></code> or <code class="docutils literal notranslate"><span class="pre">LINES</span></code> is not defined, which is the common case,1056the terminal connected to <a class="reference internal" href="sys.html#sys.__stdout__" title="sys.__stdout__"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.__stdout__</span></code></a> is queried1057by invoking <a class="reference internal" href="os.html#os.get_terminal_size" title="os.get_terminal_size"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.get_terminal_size()</span></code></a>.</p>1058<p>If the terminal size cannot be successfully queried, either because1059the system doesn’t support querying, or because we are not1060connected to a terminal, the value given in <code class="docutils literal notranslate"><span class="pre">fallback</span></code> parameter1061is used. <code class="docutils literal notranslate"><span class="pre">fallback</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">(80,</span> <span class="pre">24)</span></code> which is the default1062size used by many terminal emulators.</p>1063<p>The value returned is a named tuple of type <a class="reference internal" href="os.html#os.terminal_size" title="os.terminal_size"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.terminal_size</span></code></a>.</p>1064<p>See also: The Single UNIX Specification, Version 2,1065<a class="reference external" href="https://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003">Other Environment Variables</a>.</p>1066<div class="versionadded">1067<p><span class="versionmodified added">Added in version 3.3.</span></p>1068</div>1069<div class="versionchanged">1070<p><span class="versionmodified changed">Changed in version 3.11: </span>The <code class="docutils literal notranslate"><span class="pre">fallback</span></code> values are also used if <a class="reference internal" href="os.html#os.get_terminal_size" title="os.get_terminal_size"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.get_terminal_size()</span></code></a>1071returns zeroes.</p>1072</div>1073</dd></dl>1074 1075</section>1076</section>1077 1078 1079            <div class="clearer"></div>1080          </div>1081        </div>1082      </div>1083      <div class="sphinxsidebar" role="navigation" aria-label="Main">1084        <div class="sphinxsidebarwrapper">1085  <div>1086    <h3><a href="../contents.html">Table of Contents</a></h3>1087    <ul>1088<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> — High-level file operations</a><ul>1089<li><a class="reference internal" href="#directory-and-files-operations">Directory and files operations</a><ul>1090<li><a class="reference internal" href="#platform-dependent-efficient-copy-operations">Platform-dependent efficient copy operations</a></li>1091<li><a class="reference internal" href="#copytree-example">copytree example</a></li>1092<li><a class="reference internal" href="#rmtree-example">rmtree example</a></li>1093</ul>1094</li>1095<li><a class="reference internal" href="#archiving-operations">Archiving operations</a><ul>1096<li><a class="reference internal" href="#archiving-example">Archiving example</a></li>1097<li><a class="reference internal" href="#archiving-example-with-base-dir">Archiving example with <em>base_dir</em></a></li>1098</ul>1099</li>1100<li><a class="reference internal" href="#querying-the-size-of-the-output-terminal">Querying the size of the output terminal</a></li>1101</ul>1102</li>1103</ul>1104 1105  </div>1106  <div>1107    <h4>Previous topic</h4>1108    <p class="topless"><a href="linecache.html"1109                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">linecache</span></code> — Random access to text lines</a></p>1110  </div>1111  <div>1112    <h4>Next topic</h4>1113    <p class="topless"><a href="persistence.html"1114                          title="next chapter">Data Persistence</a></p>1115  </div>1116  <script>1117    document.addEventListener('DOMContentLoaded', () => {1118        const title = document.querySelector('meta[property="og:title"]').content;1119        const elements = document.querySelectorAll('.improvepage');1120        const pageurl = window.location.href.split('?')[0];1121        elements.forEach(element => {1122            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));1123            url.searchParams.set('pagetitle', title);1124            url.searchParams.set('pageurl', pageurl);1125            url.searchParams.set('pagesource', "library/shutil.rst");1126            element.href = url.toString();1127        });1128    });1129  </script>1130  <div role="note" aria-label="source link">1131    <h3>This page</h3>1132    <ul class="this-page-menu">1133      <li><a href="../bugs.html">Report a bug</a></li>1134      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>1135      <li>1136        <a href="https://github.com/python/cpython/blob/main/Doc/library/shutil.rst?plain=1"1137            rel="nofollow">Show source1138        </a>1139      </li>1140      1141    </ul>1142  </div>1143        </div>1144<div id="sidebarbutton" title="Collapse sidebar">1145<span>«</span>1146</div>1147 1148      </div>1149      <div class="clearer"></div>1150    </div>  1151    <div class="related" role="navigation" aria-label="Related">1152      <h3>Navigation</h3>1153      <ul>1154        <li class="right" style="margin-right: 10px">1155          <a href="../genindex.html" title="General Index"1156             >index</a></li>1157        <li class="right" >1158          <a href="../py-modindex.html" title="Python Module Index"1159             >modules</a> |</li>1160        <li class="right" >1161          <a href="persistence.html" title="Data Persistence"1162             >next</a> |</li>1163        <li class="right" >1164          <a href="linecache.html" title="linecache — Random access to text lines"1165             >previous</a> |</li>1166 1167          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>1168          <li><a href="https://www.python.org/">Python</a> &#187;</li>1169          <li class="switchers">1170            <div class="language_switcher_placeholder"></div>1171            <div class="version_switcher_placeholder"></div>1172          </li>1173          <li>1174              1175          </li>1176    <li id="cpython-language-and-version">1177      <a href="../index.html">3.15.0a6 Documentation</a> &#187;1178    </li>1179 1180          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>1181          <li class="nav-item nav-item-2"><a href="filesys.html" >File and Directory Access</a> &#187;</li>1182        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> — High-level file operations</a></li>1183                <li class="right">1184                    1185 1186    <div class="inline-search" role="search">1187        <form class="inline-search" action="../search.html" method="get">1188          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">1189          <input type="submit" value="Go">1190        </form>1191    </div>1192                     |1193                </li>1194            <li class="right">1195<label class="theme-selector-label">1196    Theme1197    <select class="theme-selector" oninput="activateTheme(this.value)">1198        <option value="auto" selected>Auto</option>1199        <option value="light">Light</option>1200        <option value="dark">Dark</option>

Showing the first 1,200 of 1233 lines. Download the file for the rest.