807 lines
96 KiB
HTML
807 lines
96 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>shutil --- 高阶文件操作 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="数据持久化" href="persistence.html" />
|
||
<link rel="prev" title="linecache --- 随机读写文本行" href="linecache.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/shutil.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
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" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> --- 高阶文件操作</a><ul>
|
||
<li><a class="reference internal" href="#directory-and-files-operations">目录和文件操作</a><ul>
|
||
<li><a class="reference internal" href="#platform-dependent-efficient-copy-operations">依赖于具体平台的高效拷贝操作</a></li>
|
||
<li><a class="reference internal" href="#copytree-example">copytree 示例</a></li>
|
||
<li><a class="reference internal" href="#rmtree-example">rmtree 示例</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#archiving-operations">归档操作</a><ul>
|
||
<li><a class="reference internal" href="#archiving-example">归档程序示例</a></li>
|
||
<li><a class="reference internal" href="#archiving-example-with-base-dir">使用 <em>base_dir</em> 的归档程序示例</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#querying-the-size-of-the-output-terminal">查询输出终端的尺寸</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="linecache.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">linecache</span></code> --- 随机读写文本行</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="persistence.html"
|
||
title="下一章">数据持久化</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/shutil.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/shutil.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="persistence.html" title="数据持久化"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="linecache.html" title="linecache --- 随机读写文本行"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">文件和目录访问</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-shutil">
|
||
<span id="shutil-high-level-file-operations"></span><h1><a class="reference internal" href="#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a> --- 高阶文件操作<a class="headerlink" href="#module-shutil" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/shutil.py">Lib/shutil.py</a></p>
|
||
<hr class="docutils" id="index-0" />
|
||
<p><a class="reference internal" href="#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a> 模块提供了一系列对文件和文件集合的高阶操作。 特别是提供了一些支持文件拷贝和删除的函数。 对于单个文件的操作,请参阅 <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> 模块。</p>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">警告</p>
|
||
<p>即便是高阶文件拷贝函数 (<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>, <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>) 也无法拷贝所有的文件元数据。</p>
|
||
<p>在 POSIX 平台上,这意味着将丢失文件所有者和组以及 ACL 数据。 在 Mac OS 上,资源钩子和其他元数据不被使用。 这意味着将丢失这些资源并且文件类型和创建者代码将不正确。 在 Windows 上,将不会拷贝文件所有者、ACL 和替代数据流。</p>
|
||
</div>
|
||
<section id="directory-and-files-operations">
|
||
<span id="file-operations"></span><h2>目录和文件操作<a class="headerlink" href="#directory-and-files-operations" title="永久链接至标题">¶</a></h2>
|
||
<dl class="function">
|
||
<dt id="shutil.copyfileobj">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">copyfileobj</code><span class="sig-paren">(</span><em class="sig-param">fsrc</em>, <em class="sig-param">fdst</em><span class="optional">[</span>, <em class="sig-param">length</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.copyfileobj" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将文件型对象 <em>fsrc</em> 的内容拷贝到文件型对象 <em>fdst</em>。 整数值 <em>length</em> 如果给出则为缓冲区大小。 特别地, <em>length</em> 为负值表示拷贝数据时不对源数据进行分块循环处理;默认情况下会分块读取数据以避免不受控制的内存消耗。 请注意如果 <em>fsrc</em> 对象的当前文件位置不为 0,则只有从当前文件位置到文件末尾的内容会被拷贝。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.copyfile">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">copyfile</code><span class="sig-paren">(</span><em class="sig-param">src</em>, <em class="sig-param">dst</em>, <em class="sig-param">*</em>, <em class="sig-param">follow_symlinks=True</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.copyfile" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将名为 <em>src</em> 的文件的内容(不包括元数据)拷贝到名为 <em>dst</em> 的文件并以尽可能高效的方式返回 <em>dst</em>。 <em>src</em> 和 <em>dst</em> 均为路径类对象或以字符串形式给出的路径名。</p>
|
||
<p><em>dst</em> 必须是完整的目标文件名;对于接受目标目录路径的拷贝请参见 <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>。 如果 <em>src</em> 和 <em>dst</em> 指定了同一个文件,则将引发 <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>。</p>
|
||
<p>目标位置必须是可写的;否则将引发 <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> 异常。 如果 <em>dst</em> 已经存在,它将被替换。 特殊文件如字符或块设备以及管道无法用此函数来拷贝。</p>
|
||
<p>如果 <em>follow_symlinks</em> 为假值且 <em>src</em> 为符号链接,则将创建一个新的符号链接而不是拷贝 <em>src</em> 所指向的文件。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copyfile</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 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> 而不是 <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>。 增加了 <em>follow_symlinks</em> 参数。 现在是返回 <em>dst</em>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.4 版更改: </span>引发 <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> 而不是 <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>。 由于前者是后者的子类,此改变是向后兼容的。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>可能会在内部使用平台专属的快速拷贝系统调用以更高效地拷贝文件。 参见 <a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">依赖于具体平台的高效拷贝操作</span></a> 一节。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="shutil.SameFileError">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">SameFileError</code><a class="headerlink" href="#shutil.SameFileError" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>此异常会在 <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> 中的源和目标为同一文件时被引发。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.4 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.copymode">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">copymode</code><span class="sig-paren">(</span><em class="sig-param">src</em>, <em class="sig-param">dst</em>, <em class="sig-param">*</em>, <em class="sig-param">follow_symlinks=True</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.copymode" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>从 <em>src</em> 拷贝权限位到 <em>dst</em>。 文件的内容、所有者和分组将不受影响。 <em>src</em> 和 <em>dst</em> 均为路径类对象或字符串形式的路径名。 如果 <em>follow_symlinks</em> 为假值,并且 <em>src</em> 和 <em>dst</em> 均为符号链接,<a class="reference internal" href="#shutil.copymode" title="shutil.copymode"><code class="xref py py-func docutils literal notranslate"><span class="pre">copymode()</span></code></a> 将尝试修改 <em>dst</em> 本身的模式(而非它所指向的文件)。 此功能并不是在所有平台上均可用;请参阅 <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> 了解详情。 如果 <a class="reference internal" href="#shutil.copymode" title="shutil.copymode"><code class="xref py py-func docutils literal notranslate"><span class="pre">copymode()</span></code></a> 无法修改本机平台上的符号链接,而它被要求这样做,它将不做任何操作即返回。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copymode</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>加入 <em>follow_symlinks</em> 参数。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.copystat">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">copystat</code><span class="sig-paren">(</span><em class="sig-param">src</em>, <em class="sig-param">dst</em>, <em class="sig-param">*</em>, <em class="sig-param">follow_symlinks=True</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.copystat" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>从 <em>src</em> 拷贝权限位、最近访问时间、最近修改时间以及旗标到 <em>dst</em>。 在 Linux上,<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> 还会在可能的情况下拷贝“扩展属性”。 文件的内容、所有者和分组将不受影响。 <em>src</em> 和 <em>dst</em> 均为路径类对象或字符串形式的路径名。</p>
|
||
<p>如果 <em>follow_symlinks</em> 为假值,并且 <em>src</em> 和 <em>dst</em> 均指向符号链接,<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> 将作用于符号链接本身而非该符号链接所指向的文件 — 从 <em>src</em> 符号链接读取信息,并将信息写入 <em>dst</em> 符号链接。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>并非所有平台者提供检查和修改符号链接的功能。 Python 本身可以告诉你哪些功能是在本机上可用的。</p>
|
||
<ul class="simple">
|
||
<li><p>如果 <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> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则 <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> 可以修改符号链接的权限位。</p></li>
|
||
<li><p>如果 <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> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则 <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> 可以修改符号链接的最近访问和修改时间。</p></li>
|
||
<li><p>如果 <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> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则 <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> 可以修改符号链接的旗标。 (<code class="docutils literal notranslate"><span class="pre">os.chflags</span></code> 不是在所有平台上均可用。)</p></li>
|
||
</ul>
|
||
<p>在此功能部分或全部不可用的平台上,当被要求修改一个符号链接时,<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> 将尽量拷贝所有内容。 <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> 一定不会返回失败信息。</p>
|
||
<p>更多信息请参阅 <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>。</p>
|
||
</div>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copystat</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>添加了 <em>follow_symlinks</em> 参数并且支持 Linux 扩展属性。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.copy">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">copy</code><span class="sig-paren">(</span><em class="sig-param">src</em>, <em class="sig-param">dst</em>, <em class="sig-param">*</em>, <em class="sig-param">follow_symlinks=True</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.copy" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将文件 <em>src</em> 拷贝到文件或目录 <em>dst</em>。 <em>src</em> 和 <em>dst</em> 应为 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">路径类对象</span></a> 或字符串。 如果 <em>dst</em> 指定了一个目录,文件将使用 <em>src</em> 中的基准文件名拷贝到 <em>dst</em> 中。 将返回新创建文件所对应的路径。</p>
|
||
<p>如果 <em>follow_symlinks</em> 为假值且 <em>src</em> 为符号链接,则 <em>dst</em> 也将被创建为符号链接。 如果 <em>follow_symlinks</em> 为真值且 <em>src</em> 为符号链接,<em>dst</em> 将成为 <em>src</em> 所指向的文件的一个副本。</p>
|
||
<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> 会拷贝文件数据和文件的权限模式 (参见 <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>)。 其他元数据,例如文件的创建和修改时间不会被保留。 要保留所有原有的元数据,请改用 <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>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copyfile</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copymode</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>添加了 <em>follow_symlinks</em> 参数。 现在会返回新创建文件的路径。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>可能会在内部使用平台专属的快速拷贝系统调用以更高效地拷贝文件。 参见 <a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">依赖于具体平台的高效拷贝操作</span></a> 一节。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.copy2">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">copy2</code><span class="sig-paren">(</span><em class="sig-param">src</em>, <em class="sig-param">dst</em>, <em class="sig-param">*</em>, <em class="sig-param">follow_symlinks=True</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.copy2" title="永久链接至目标">¶</a></dt>
|
||
<dd><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>,区别在于 <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>
|
||
<p>当 <em>follow_symlinks</em> 为假值且 <em>src</em> 为符号链接时,<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> 会尝试将来自 <em>src</em> 符号链接的所有元数据拷贝到新创建的 <em>dst</em> 符号链接。 但是,此功能不是在所有平台上均可用。 在此功能部分或全部不可用的平台上,<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> 将尽量保留所有元数据;<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>
|
||
<p><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> 会使用 <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> 来拷贝文件元数据。 请参阅 <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> 了解有关修改符号链接元数据的平台支持的更多信息。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copyfile</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copystat</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>添加了 <em>follow_symlinks</em> 参数,还会尝试拷贝扩展文件系统属性(目前仅限 Linux)。 现在会返回新创建文件的路径。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>可能会在内部使用平台专属的快速拷贝系统调用以更高效地拷贝文件。 参见 <a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">依赖于具体平台的高效拷贝操作</span></a> 一节。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.ignore_patterns">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">ignore_patterns</code><span class="sig-paren">(</span><em class="sig-param">*patterns</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.ignore_patterns" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>这个工厂函数会创建一个函数,它可被用作 <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> 的 <em>ignore</em> 可调用对象参数,以忽略那些匹配所提供的 glob 风格的 <em>patterns</em> 之一的文件和目录。 参见以下示例。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.copytree">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">copytree</code><span class="sig-paren">(</span><em class="sig-param">src</em>, <em class="sig-param">dst</em>, <em class="sig-param">symlinks=False</em>, <em class="sig-param">ignore=None</em>, <em class="sig-param">copy_function=copy2</em>, <em class="sig-param">ignore_dangling_symlinks=False</em>, <em class="sig-param">dirs_exist_ok=False</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.copytree" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将以 <em>src</em> 为根起点的整个目录树拷贝到名为 <em>dst</em> 的目录并返回目标目录。 <em>dirs_exist_ok</em> 指明是否要在 <em>dst</em> 或任何丢失的父目录已存在的情况下引发异常。</p>
|
||
<p>目录的权限和时间会通过 <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> 来拷贝,单个文件则会使用 <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>
|
||
<p>如果 <em>symlinks</em> 为真值,源目录树中的符号链接会在新目录树中表示为符号链接,并且原链接的元数据在平台允许的情况下也会被拷贝;如果为假值或省略,则会将被链接文件的内容和元数据拷贝到新目录树。</p>
|
||
<p>当 <em>symlinks</em> 为假值时,如果符号链接所指向的文件不存在,则会在拷贝进程的末尾将一个异常添加到 <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> 异常中的错误列表。 如果你希望屏蔽此异常那就将可选的 <em>ignore_dangling_symlinks</em> 旗标设为真值。 请注意此选项在不支持 <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>
|
||
<p>如果给出了 <em>ignore</em>,它必须是一个可调用对象,该对象将接受 <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> 所访问的目录以及 <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> 所返回的目录内容列表作为其参数。 由于 <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> 是递归地被调用的,<em>ignore</em> 可调用对象对于每个被拷贝目录都将被调用一次。 该可调用对象必须返回一个相对于当前目录的目录和文件名序列(即其第二个参数的子集);随后这些名称将在拷贝进程中被忽略。 <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> 可被用于创建这种基于 glob 风格模式来忽略特定名称的可调用对象。</p>
|
||
<p>如果发生了(一个或多个)异常,将引发一个附带原因列表的 <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>。</p>
|
||
<p>如果给出了 <em>copy_function</em>,它必须是一个将被用来拷贝每个文件的可调用对象。 它在被调用时会将源路径和目标路径作为参数传入。 默认情况下,<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> 将被使用,但任何支持同样签名(与 <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> 一致)都可以使用。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.copytree</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>当 <em>symlinks</em> 为假值时拷贝元数据。 现在会返回 <em>dst</em>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了 <em>copy_function</em> 参数以允许提供定制的拷贝函数。 添加了 <em>ignore_dangling_symlinks</em> 参数以便在 <em>symlinks</em> 为假值时屏蔽符号链接错误。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>可能会在内部使用平台专属的快速拷贝系统调用以更高效地拷贝文件。 参见 <a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">依赖于具体平台的高效拷贝操作</span></a> 一节。</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.8 新版功能: </span><em>dirs_exist_ok</em> 形参。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.rmtree">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">rmtree</code><span class="sig-paren">(</span><em class="sig-param">path</em>, <em class="sig-param">ignore_errors=False</em>, <em class="sig-param">onerror=None</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.rmtree" title="永久链接至目标">¶</a></dt>
|
||
<dd><p id="index-1">删除一个完整的目录树;<em>path</em> 必须指向一个目录(但不能是一个目录的符号链接)。 如果 <em>ignore_errors</em> 为真值,删除失败导致的错误将被忽略;如果为假值或是省略,此类错误将通过调用由 <em>onerror</em> 所指定的处理程序来处理,或者如果此参数被省略则将引发一个异常。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>在支持必要的基于 fd 的函数的平台上,默认会使用 <a class="reference internal" href="#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmtree()</span></code></a> 的可防御符号链接攻击的版本。 在其他平台上,<a class="reference internal" href="#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmtree()</span></code></a> 较易遭受符号链接攻击:给定适当的时间和环境,攻击者可以操纵文件系统中的符号链接来删除他们在其他情况下无法访问的文件。 应用程序可以使用 <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> 函数属性来确定此类情况具体是哪一些。</p>
|
||
</div>
|
||
<p>如果提供了 <em>onerror</em>,它必须为接受三个形参的可调用对象: <em>function</em>, <em>path</em> 和 <em>excinfo</em>。</p>
|
||
<p>第一个形参 <em>function</em> 是引发异常的函数;它依赖于具体的平台和实现。 第二个形参 <em>path</em> 将是传递给 <em>function</em> 的路径名。 第三个形参 <em>excinfo</em> 将是由 <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> 所返回的异常信息。 由 <em>onerror</em> 所引发的异常将不会被捕获。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.rmtree</span></code> 附带参数 <code class="docutils literal notranslate"><span class="pre">path</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>添加了一个防御符号链接攻击的版本,如果平台支持基于 fd 的函数就会被使用。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>在 Windows 上将不会再在移除连接之前删除目录连接中的内容。</p>
|
||
</div>
|
||
<dl class="attribute">
|
||
<dt id="shutil.rmtree.avoids_symlink_attacks">
|
||
<code class="sig-prename descclassname">rmtree.</code><code class="sig-name descname">avoids_symlink_attacks</code><a class="headerlink" href="#shutil.rmtree.avoids_symlink_attacks" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>指明当前平台和实现是否提供防御符号链接攻击的 <a class="reference internal" href="#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmtree()</span></code></a> 版本。 目前它仅在平台支持基于 fd 的目录访问函数时才返回真值。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.move">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">move</code><span class="sig-paren">(</span><em class="sig-param">src</em>, <em class="sig-param">dst</em>, <em class="sig-param">copy_function=copy2</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.move" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>递归地将一个文件或目录 (<em>src</em>) 移至另一位置 (<em>dst</em>) 并返回目标位置。</p>
|
||
<p>如果目标是已存在的目录,则 <em>src</em> 会被移至该目录下。 如果目标已存在但不是目录,它可能会被覆盖,具体取决于 <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> 的语义。</p>
|
||
<p>如果目标是在当前文件系统中,则会使用 <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>。 在其他情况下,<em>src</em> 将被拷贝至 <em>dst</em>,使用的函数为 <em>copy_function</em>,然后目标会被移除。 对于符号链接,则将在 <em>dst</em> 之下或以其本身为名称创建一个指向 <em>src</em> 目标的新符号链接,并且 <em>src</em> 将被移除。</p>
|
||
<p>如果给出了 <em>copy_function</em>,则它必须为接受两个参数 <em>src</em> 和 <em>dst</em> 的可调用对象,并将在 <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> 无法使用时被用来将 <em>src</em> 拷贝到 <em>dst</em>。 如果源是一个目录,则会调用 <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>,并向它传入 <code class="xref py py-func docutils literal notranslate"><span class="pre">copy_function()</span></code>。 默认的 <em>copy_function</em> 是 <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>。 使用 <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> 作为 <em>copy_function</em> 允许在无法附带拷贝元数据时让移动操作成功执行,但其代价是不拷贝任何元数据。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.move</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">src</span></code>, <code class="docutils literal notranslate"><span class="pre">dst</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>为异类文件系统添加了显式的符号链接处理,以便使它适应 GNU 的 <strong class="program">mv</strong> 的行为。 现在会返回 <em>dst</em>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span>增加了 <em>copy_function</em> 关键字参数。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>可能会在内部使用平台专属的快速拷贝系统调用以更高效地拷贝文件。 参见 <a class="reference internal" href="#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">依赖于具体平台的高效拷贝操作</span></a> 一节。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.disk_usage">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">disk_usage</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.disk_usage" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回给定路径的磁盘使用统计数据,形式为一个 <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a>,其中包含 <em>total</em>, <em>used</em> 和 <em>free</em> 属性,分别表示总计、已使用和未使用空间的字节数。 <em>path</em> 可以是一个文件或是一个目录。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>在 Windows 上,<em>path</em> 现在可以是一个文件或目录。</p>
|
||
</div>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: Unix, Windows。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.chown">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">chown</code><span class="sig-paren">(</span><em class="sig-param">path</em>, <em class="sig-param">user=None</em>, <em class="sig-param">group=None</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.chown" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>修改给定 <em>path</em> 的所有者 <em>user</em> 和/或 <em>group</em>。</p>
|
||
<p><em>user</em> 可以是一个系统用户名或 uid;<em>group</em> 同样如此。 要求至少有一个参数。</p>
|
||
<p>另请参阅下层的函数 <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>。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.chown</span></code> 并附带参数 <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>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: Unix。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.which">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">which</code><span class="sig-paren">(</span><em class="sig-param">cmd</em>, <em class="sig-param">mode=os.F_OK | os.X_OK</em>, <em class="sig-param">path=None</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.which" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回当给定的 <em>cmd</em> 被调用时将要运行的可执行文件的路径。 如果没有 <em>cmd</em> 会被调用则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
<p><em>mode</em> 是一个传递给 <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> 的权限掩码,在默认情况下将确定文件是否存在并且为可执行文件。</p>
|
||
<p>当未指定 <em>path</em> 时,将会使用 <a class="reference internal" href="os.html#os.environ" title="os.environ"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.environ()</span></code></a> 的结果,返回 "PATH" 的值或回退为 <a class="reference internal" href="os.html#os.defpath" title="os.defpath"><code class="xref py py-attr docutils literal notranslate"><span class="pre">os.defpath</span></code></a>。</p>
|
||
<p>在 Windows 上当前目录总是会被添加为 <em>path</em> 的第一项,无论你是否使用默认值或提供你自己的路径,这是命令行终端在查找可执行文件时所采用的行为方式。 此外,当在 <em>path</em> 中查找 <em>cmd</em> 时,还会检查 <code class="docutils literal notranslate"><span class="pre">PATHEXT</span></code> 环境变量。 例如,如果你调用 <code class="docutils literal notranslate"><span class="pre">shutil.which("python")</span></code>,<a class="reference internal" href="#shutil.which" title="shutil.which"><code class="xref py py-func docutils literal notranslate"><span class="pre">which()</span></code></a> 将搜索 <code class="docutils literal notranslate"><span class="pre">PATHEXT</span></code> 来确定它要在 <em>path</em> 目录中查找 <code class="docutils literal notranslate"><span class="pre">python.exe</span></code>。 例如,在 Windows 上:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">shutil</span><span class="o">.</span><span class="n">which</span><span class="p">(</span><span class="s2">"python"</span><span class="p">)</span>
|
||
<span class="go">'C:\\Python33\\python.EXE'</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>现在可以接受 <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> 类型。 如果 <em>cmd</em> 的类型为 <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>,结果的类型也将为 <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>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="exception">
|
||
<dt id="shutil.Error">
|
||
<em class="property">exception </em><code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">Error</code><a class="headerlink" href="#shutil.Error" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>此异常会收集在多文件操作期间所引发的异常。 对于 <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>,此异常参数将是一个由三元组 (<em>srcname</em>, <em>dstname</em>, <em>exception</em>) 构成的列表。</p>
|
||
</dd></dl>
|
||
|
||
<section id="platform-dependent-efficient-copy-operations">
|
||
<span id="shutil-platform-dependent-efficient-copy-operations"></span><h3>依赖于具体平台的高效拷贝操作<a class="headerlink" href="#platform-dependent-efficient-copy-operations" title="永久链接至标题">¶</a></h3>
|
||
<p>从 Python 3.8 开始,所有涉及文件拷贝的函数 (<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>, <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> 以及 <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>) 将会使用平台专属的 "fast-copy" 系统调用以便更高效地拷贝文件 (参见 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33671">bpo-33671</a>)。 "fast-copy" 意味着拷贝操作将发生于内核之中,避免像在 "<code class="docutils literal notranslate"><span class="pre">outfd.write(infd.read())</span></code>" 中那样使用 Python 用户空间的缓冲区。</p>
|
||
<p>在 macOS 上将会使用 <a class="reference external" href="http://www.manpagez.com/man/3/copyfile/">fcopyfile</a> 来拷贝文件内容(不含元数据)。</p>
|
||
<p>在 Linux 上将会使用 <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>
|
||
<p>在 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> 将会使用更大的默认缓冲区(1 MiB 而非 64 KiB)并且会使用基于 <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> 的 <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> 变种形式。</p>
|
||
<p>如果快速拷贝操作失败并且没有数据被写入目标文件,则 shutil 将在内部静默地回退到使用效率较低的 <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> 函数。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改.</span></p>
|
||
</div>
|
||
</section>
|
||
<section id="copytree-example">
|
||
<span id="shutil-copytree-example"></span><h3>copytree 示例<a class="headerlink" href="#copytree-example" title="永久链接至标题">¶</a></h3>
|
||
<p>这个示例就是上面所描述的 <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> 函数的实现,其中省略了文档字符串。 它还展示了此模块所提供的许多其他函数。</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">copytree</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dst</span><span class="p">,</span> <span class="n">symlinks</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
|
||
<span class="n">names</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">src</span><span class="p">)</span>
|
||
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">dst</span><span class="p">)</span>
|
||
<span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
|
||
<span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span><span class="p">:</span>
|
||
<span class="n">srcname</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">join</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
|
||
<span class="n">dstname</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">join</span><span class="p">(</span><span class="n">dst</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
|
||
<span class="k">try</span><span class="p">:</span>
|
||
<span class="k">if</span> <span class="n">symlinks</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">islink</span><span class="p">(</span><span class="n">srcname</span><span class="p">):</span>
|
||
<span class="n">linkto</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">readlink</span><span class="p">(</span><span class="n">srcname</span><span class="p">)</span>
|
||
<span class="n">os</span><span class="o">.</span><span class="n">symlink</span><span class="p">(</span><span class="n">linkto</span><span class="p">,</span> <span class="n">dstname</span><span class="p">)</span>
|
||
<span class="k">elif</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">srcname</span><span class="p">):</span>
|
||
<span class="n">copytree</span><span class="p">(</span><span class="n">srcname</span><span class="p">,</span> <span class="n">dstname</span><span class="p">,</span> <span class="n">symlinks</span><span class="p">)</span>
|
||
<span class="k">else</span><span class="p">:</span>
|
||
<span class="n">copy2</span><span class="p">(</span><span class="n">srcname</span><span class="p">,</span> <span class="n">dstname</span><span class="p">)</span>
|
||
<span class="c1"># XXX What about devices, sockets etc.?</span>
|
||
<span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">why</span><span class="p">:</span>
|
||
<span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">srcname</span><span class="p">,</span> <span class="n">dstname</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">why</span><span class="p">)))</span>
|
||
<span class="c1"># catch the Error from the recursive copytree so that we can</span>
|
||
<span class="c1"># continue with other files</span>
|
||
<span class="k">except</span> <span class="n">Error</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
|
||
<span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">err</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
|
||
<span class="k">try</span><span class="p">:</span>
|
||
<span class="n">copystat</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dst</span><span class="p">)</span>
|
||
<span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">why</span><span class="p">:</span>
|
||
<span class="c1"># can't copy file access times on Windows</span>
|
||
<span class="k">if</span> <span class="n">why</span><span class="o">.</span><span class="n">winerror</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
|
||
<span class="n">errors</span><span class="o">.</span><span class="n">extend</span><span class="p">((</span><span class="n">src</span><span class="p">,</span> <span class="n">dst</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">why</span><span class="p">)))</span>
|
||
<span class="k">if</span> <span class="n">errors</span><span class="p">:</span>
|
||
<span class="k">raise</span> <span class="n">Error</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>另一个使用 <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> 辅助函数的例子:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">shutil</span> <span class="kn">import</span> <span class="n">copytree</span><span class="p">,</span> <span class="n">ignore_patterns</span>
|
||
|
||
<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">'*.pyc'</span><span class="p">,</span> <span class="s1">'tmp*'</span><span class="p">))</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>这将会拷贝除 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件和以 <code class="docutils literal notranslate"><span class="pre">tmp</span></code> 打头的文件或目录以外的所有条目.</p>
|
||
<p>另一个使用 <em>ignore</em> 参数来添加记录调用的例子:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">shutil</span> <span class="kn">import</span> <span class="n">copytree</span>
|
||
<span class="kn">import</span> <span class="nn">logging</span>
|
||
|
||
<span class="k">def</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>
|
||
<span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'Working in </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
|
||
<span class="k">return</span> <span class="p">[]</span> <span class="c1"># nothing will be ignored</span>
|
||
|
||
<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>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="rmtree-example">
|
||
<span id="shutil-rmtree-example"></span><h3>rmtree 示例<a class="headerlink" href="#rmtree-example" title="永久链接至标题">¶</a></h3>
|
||
<p>这个例子演示了如何在 Windows 上删除一个目录树,其中部分文件设置了只读属性位。 它会使用 onerror 回调函数来清除只读属性位并再次尝试删除。 任何后续的失败都将被传播。</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">stat</span>
|
||
<span class="kn">import</span> <span class="nn">shutil</span>
|
||
|
||
<span class="k">def</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>
|
||
<span class="s2">"Clear the readonly bit and reattempt the removal"</span>
|
||
<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>
|
||
<span class="n">func</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
|
||
|
||
<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">onerror</span><span class="o">=</span><span class="n">remove_readonly</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="archiving-operations">
|
||
<span id="id1"></span><h2>归档操作<a class="headerlink" href="#archiving-operations" title="永久链接至标题">¶</a></h2>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span>添加了对 <em>xztar</em> 格式的支持。</p>
|
||
</div>
|
||
<p>本模块也提供了用于创建和读取压缩和归档文件的高层级工具。 它们依赖于 <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> 和 <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> 模块。</p>
|
||
<dl class="function">
|
||
<dt id="shutil.make_archive">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">make_archive</code><span class="sig-paren">(</span><em class="sig-param">base_name</em>, <em class="sig-param">format</em><span class="optional">[</span>, <em class="sig-param">root_dir</em><span class="optional">[</span>, <em class="sig-param">base_dir</em><span class="optional">[</span>, <em class="sig-param">verbose</em><span class="optional">[</span>, <em class="sig-param">dry_run</em><span class="optional">[</span>, <em class="sig-param">owner</em><span class="optional">[</span>, <em class="sig-param">group</em><span class="optional">[</span>, <em class="sig-param">logger</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="永久链接至目标">¶</a></dt>
|
||
<dd><p>创建一个归档文件(例如 zip 或 tar)并返回其名称。</p>
|
||
<p><em>base_name</em> 是要创建的文件名称,包括路径,去除任何特定格式的扩展名。 <em>format</em> 是归档格式:为 "zip" (如果 <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> 模块可用), "tar", "gztar" (如果 <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> 模块可用), "bztar" (如果 <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> 模块可用) 或 "xztar" (如果 <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> 模块可用) 中的一个。</p>
|
||
<p><em>root_dir</em> 是一个目录,它将作为归档文件的根目录,归档中的所有路径都将是它的相对路径;例如,我们通常会在创建归档之前用 chdir 命令切换到 <em>root_dir</em>。</p>
|
||
<p><em>base_dir</em> 是我们要执行归档的起始目录;也就是说 <em>base_dir</em> 将成为归档中所有文件和目录共有的路径前缀。 <em>base_dir</em> 必须相对于 <em>root_dir</em> 给出。 请参阅 <a class="reference internal" href="#shutil-archiving-example-with-basedir"><span class="std std-ref">使用 base_dir 的归档程序示例</span></a> 了解如何同时使用 <em>base_dir</em> 和 <em>root_dir</em>。</p>
|
||
<p><em>root_dir</em> 和 <em>base_dir</em> 默认均为当前目录。</p>
|
||
<p>如果 <em>dry_run</em> 为真值,则不会创建归档文件,但将要被执行的操作会被记录到 <em>logger</em>。</p>
|
||
<p><em>owner</em> 和 <em>group</em> 将在创建 tar 归档文件时被使用。 默认会使用当前的所有者和分组。</p>
|
||
<p><em>logger</em> 必须是一个兼容 <span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0282"><strong>PEP 282</strong></a> 的对象,通常为 <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>
|
||
<p><em>verbose</em> 参数已不再使用并进入弃用状态。</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.make_archive</span></code> 并附带参数 <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>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>这个函数不是线程安全的。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>现在对于通过 <code class="docutils literal notranslate"><span class="pre">format="tar"</span></code> 创建的归档文件将使用新式的 pax (POSIX.1-2001) 格式而非旧式的 GNU 格式。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.get_archive_formats">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">get_archive_formats</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.get_archive_formats" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回支持的归档格式列表。 所返回序列中的每个元素为一个元组 <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">description)</span></code>。</p>
|
||
<p>默认情况下 <a class="reference internal" href="#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a> 提供以下格式:</p>
|
||
<ul class="simple">
|
||
<li><p><em>zip</em>: ZIP 文件(如果 <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> 模块可用)。</p></li>
|
||
<li><p><em>tar</em>: 未压缩的 tar 文件。 对于新归档文件将使用 POSIX.1-2001 pax 格式。</p></li>
|
||
<li><p><em>gztar</em>: gzip 压缩的 tar 文件(如果 <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> 模块可用)。</p></li>
|
||
<li><p><em>bztar</em>: bzip2 压缩的 tar 文件(如果 <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> 模块可用)。</p></li>
|
||
<li><p><em>xztar</em>: xz 压缩的 tar 文件(如果 <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> 模块可用)。</p></li>
|
||
</ul>
|
||
<p>你可以通过使用 <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>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.register_archive_format">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">register_archive_format</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">function</em><span class="optional">[</span>, <em class="sig-param">extra_args</em><span class="optional">[</span>, <em class="sig-param">description</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.register_archive_format" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>为 <em>name</em> 格式注册一个归档器。</p>
|
||
<p><em>function</em> 是将被用来解包归档文件的可调用对象。 该可调用对象将接收要创建文件的 <em>base_name</em>,再加上要归档内容的 <em>base_dir</em> (其默认值为 <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>)。 更多参数会被作为关键字参数传入: <em>owner</em>, <em>group</em>, <em>dry_run</em> 和 <em>logger</em> (与向 <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>
|
||
<p>如果给出了 <em>extra_args</em>,则其应为一个 <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> 对的序列,将在归档器可调用对象被使用时作为附加的关键字参数。</p>
|
||
<p><em>description</em> 由 <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> 使用,它将返回归档器的列表。 默认值为一个空字符串。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.unregister_archive_format">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">unregister_archive_format</code><span class="sig-paren">(</span><em class="sig-param">name</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.unregister_archive_format" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>从支持的格式中移除归档格式 <em>name</em>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.unpack_archive">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">unpack_archive</code><span class="sig-paren">(</span><em class="sig-param">filename</em><span class="optional">[</span>, <em class="sig-param">extract_dir</em><span class="optional">[</span>, <em class="sig-param">format</em><span class="optional">[</span>, <em class="sig-param">filter</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="永久链接至目标">¶</a></dt>
|
||
<dd><p>解包一个归档文件。 <em>filename</em> 是归档文件的完整路径。</p>
|
||
<p><em>extract_dir</em> 是归档文件解包的目标目录名称。 如果未提供,则将使用当前工作目录。</p>
|
||
<p><em>format</em> 是归档格式:应为 "zip", "tar", "gztar", "bztar" 或 "xztar" 之一。 或者任何通过 <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> 注册的其他格式。 如果未提供,<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> 将使用归档文件的扩展名来检查是否注册了对应于该扩展名的解包器。 在未找到任何解包器的情况下,将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
|
||
<p>The keyword-only <em>filter</em> argument, which was added in Python 3.8.17,
|
||
is passed to the underlying unpacking function.
|
||
For zip files, <em>filter</em> is not accepted.
|
||
For tar files, it is recommended to set it to <code class="docutils literal notranslate"><span class="pre">'data'</span></code>,
|
||
unless using features specific to tar and UNIX-like filesystems.
|
||
(See <a class="reference internal" href="tarfile.html#tarfile-extraction-filter"><span class="std std-ref">解压缩过滤器</span></a> for details.)
|
||
The <code class="docutils literal notranslate"><span class="pre">'data'</span></code> filter will become the default for tar files
|
||
in Python 3.14.</p>
|
||
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">shutil.unpack_archive</span></code> 并附带参数 <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>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">警告</p>
|
||
<p>绝不要未经预先检验就从不可靠的源中提取归档文件。 这样有可能会在 <em>extract_dir</em> 参数所指定的路径之外创建文件,例如某些成员具有以 "/" 打头的绝对路径文件名或是以两个点号 ".." 打头的文件名。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> 作为 <em>filename</em> 和 <em>extract_dir</em>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8.17 版更改: </span>增加了 <em>filter</em> 参数。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.register_unpack_format">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">register_unpack_format</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">extensions</em>, <em class="sig-param">function</em><span class="optional">[</span>, <em class="sig-param">extra_args</em><span class="optional">[</span>, <em class="sig-param">description</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.register_unpack_format" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>注册一个解包格式。 <em>name</em> 为格式名称而 <em>extensions</em> 为对应于该格式的扩展名列表,例如 Zip 文件的扩展名为 <code class="docutils literal notranslate"><span class="pre">.zip</span></code>。</p>
|
||
<p><em>function</em> 是将被用于解包归档的可调用对象。 该可调用对象将接受:</p>
|
||
<ul class="simple">
|
||
<li><p>归档的路径,为位置参数;</p></li>
|
||
<li><p>归档要提取到的目录,为位置参数;</p></li>
|
||
<li><p>可选的 <em>filter</em> 关键字参数,如果有提供给 <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>
|
||
<li><p>额外的关键字参数,由 <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> 元组组成的序列 <em>extra_args</em> 指明。</p></li>
|
||
</ul>
|
||
<p>可以提供 <em>description</em> 来描述该格式,它将被 <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> 返回。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.unregister_unpack_format">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">unregister_unpack_format</code><span class="sig-paren">(</span><em class="sig-param">name</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.unregister_unpack_format" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>撤销注册一个解包格式。 <em>name</em> 为格式的名称。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shutil.get_unpack_formats">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">get_unpack_formats</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shutil.get_unpack_formats" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回所有已注册的解包格式列表。 所返回序列中的每个元素为一个元组 <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">extensions,</span> <span class="pre">description)</span></code>。</p>
|
||
<p>默认情况下 <a class="reference internal" href="#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a> 提供以下格式:</p>
|
||
<ul class="simple">
|
||
<li><p><em>zip</em>: ZIP 文件(只有在相应模块可用时才能解包压缩文件)。</p></li>
|
||
<li><p><em>tar</em>: 未压缩的 tar 文件。</p></li>
|
||
<li><p><em>gztar</em>: gzip 压缩的 tar 文件(如果 <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> 模块可用)。</p></li>
|
||
<li><p><em>bztar</em>: bzip2 压缩的 tar 文件(如果 <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> 模块可用)。</p></li>
|
||
<li><p><em>xztar</em>: xz 压缩的 tar 文件(如果 <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> 模块可用)。</p></li>
|
||
</ul>
|
||
<p>你可以通过使用 <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>
|
||
</dd></dl>
|
||
|
||
<section id="archiving-example">
|
||
<span id="shutil-archiving-example"></span><h3>归档程序示例<a class="headerlink" href="#archiving-example" title="永久链接至标题">¶</a></h3>
|
||
<p>在这个示例中,我们创建了一个 gzip 压缩的 tar 归档文件,其中包含用户的 <code class="file docutils literal notranslate"><span class="pre">.ssh</span></code> 目录下的所有文件:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">shutil</span> <span class="kn">import</span> <span class="n">make_archive</span>
|
||
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">os</span>
|
||
<span class="gp">>>> </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">'~'</span><span class="p">,</span> <span class="s1">'myarchive'</span><span class="p">))</span>
|
||
<span class="gp">>>> </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">'~'</span><span class="p">,</span> <span class="s1">'.ssh'</span><span class="p">))</span>
|
||
<span class="gp">>>> </span><span class="n">make_archive</span><span class="p">(</span><span class="n">archive_name</span><span class="p">,</span> <span class="s1">'gztar'</span><span class="p">,</span> <span class="n">root_dir</span><span class="p">)</span>
|
||
<span class="go">'/Users/tarek/myarchive.tar.gz'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>结果归档文件中包含有:</p>
|
||
<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.gz
|
||
<span class="go">drwx------ tarek/staff 0 2010-02-01 16:23:40 ./</span>
|
||
<span class="go">-rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys</span>
|
||
<span class="go">-rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config</span>
|
||
<span class="go">-rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa</span>
|
||
<span class="go">-rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub</span>
|
||
<span class="go">-rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa</span>
|
||
<span class="go">-rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub</span>
|
||
<span class="go">-rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="archiving-example-with-base-dir">
|
||
<span id="shutil-archiving-example-with-basedir"></span><h3>使用 <em>base_dir</em> 的归档程序示例<a class="headerlink" href="#archiving-example-with-base-dir" title="永久链接至标题">¶</a></h3>
|
||
<p>在这个例子中,与 <a class="reference internal" href="#shutil-archiving-example">上面的例子</a> 类似,我们演示了如何使用 <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>,但这次是使用 <em>base_dir</em>。 我们现在具有如下的目录结构:</p>
|
||
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>tree<span class="w"> </span>tmp
|
||
<span class="go">tmp</span>
|
||
<span class="go">└── root</span>
|
||
<span class="go"> └── structure</span>
|
||
<span class="go"> ├── content</span>
|
||
<span class="go"> └── please_add.txt</span>
|
||
<span class="go"> └── do_not_add.txt</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>在最终的归档中,应当会包括 <code class="file docutils literal notranslate"><span class="pre">please_add.txt</span></code>,但不应当包括 <code class="file docutils literal notranslate"><span class="pre">do_not_add.txt</span></code>。 因此我们使用以下代码:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">shutil</span> <span class="kn">import</span> <span class="n">make_archive</span>
|
||
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">os</span>
|
||
<span class="gp">>>> </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">'~'</span><span class="p">,</span> <span class="s1">'myarchive'</span><span class="p">))</span>
|
||
<span class="gp">>>> </span><span class="n">make_archive</span><span class="p">(</span>
|
||
<span class="gp">... </span> <span class="n">archive_name</span><span class="p">,</span>
|
||
<span class="gp">... </span> <span class="s1">'tar'</span><span class="p">,</span>
|
||
<span class="gp">... </span> <span class="n">root_dir</span><span class="o">=</span><span class="s1">'tmp/root'</span><span class="p">,</span>
|
||
<span class="gp">... </span> <span class="n">base_dir</span><span class="o">=</span><span class="s1">'structure/content'</span><span class="p">,</span>
|
||
<span class="gp">... </span><span class="p">)</span>
|
||
<span class="go">'/Users/tarek/my_archive.tar'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>列出结果归档中的文件我们将会得到:</p>
|
||
<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.tar
|
||
<span class="go">structure/content/</span>
|
||
<span class="go">structure/content/please_add.txt</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="querying-the-size-of-the-output-terminal">
|
||
<h2>查询输出终端的尺寸<a class="headerlink" href="#querying-the-size-of-the-output-terminal" title="永久链接至标题">¶</a></h2>
|
||
<dl class="function">
|
||
<dt id="shutil.get_terminal_size">
|
||
<code class="sig-prename descclassname">shutil.</code><code class="sig-name descname">get_terminal_size</code><span class="sig-paren">(</span><em class="sig-param">fallback=(columns</em>, <em class="sig-param">lines)</em><span class="sig-paren">)</span><a class="headerlink" href="#shutil.get_terminal_size" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>获取终端窗口的尺寸。</p>
|
||
<p>对于两个维度中的每一个,会分别检查环境变量 <code class="docutils literal notranslate"><span class="pre">COLUMNS</span></code> 和 <code class="docutils literal notranslate"><span class="pre">LINES</span></code>。 如果定义了这些变量并且其值为正整数,则将使用这些值。</p>
|
||
<p>如果未定义 <code class="docutils literal notranslate"><span class="pre">COLUMNS</span></code> 或 <code class="docutils literal notranslate"><span class="pre">LINES</span></code>,这是通常的情况,则连接到 <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> 的终端将通过发起调用 <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>
|
||
<p>如果由于系统不支持查询,或是由于我们未连接到某个终端而导致查询终端尺寸不成功,则会使用在 <code class="docutils literal notranslate"><span class="pre">fallback</span></code> 形参中给出的值。 <code class="docutils literal notranslate"><span class="pre">fallback</span></code> 默认为 <code class="docutils literal notranslate"><span class="pre">(80,</span> <span class="pre">24)</span></code>,这是许多终端模拟器所使用的默认尺寸。</p>
|
||
<p>返回的值是一个 <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>
|
||
<p>另请参阅: The Single UNIX Specification, Version 2, <a class="reference external" href="http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003">Other Environment Variables</a>.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code> --- 高阶文件操作</a><ul>
|
||
<li><a class="reference internal" href="#directory-and-files-operations">目录和文件操作</a><ul>
|
||
<li><a class="reference internal" href="#platform-dependent-efficient-copy-operations">依赖于具体平台的高效拷贝操作</a></li>
|
||
<li><a class="reference internal" href="#copytree-example">copytree 示例</a></li>
|
||
<li><a class="reference internal" href="#rmtree-example">rmtree 示例</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#archiving-operations">归档操作</a><ul>
|
||
<li><a class="reference internal" href="#archiving-example">归档程序示例</a></li>
|
||
<li><a class="reference internal" href="#archiving-example-with-base-dir">使用 <em>base_dir</em> 的归档程序示例</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#querying-the-size-of-the-output-terminal">查询输出终端的尺寸</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="linecache.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">linecache</span></code> --- 随机读写文本行</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="persistence.html"
|
||
title="下一章">数据持久化</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/shutil.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="persistence.html" title="数据持久化"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="linecache.html" title="linecache --- 随机读写文本行"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="filesys.html" >文件和目录访问</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |