597 lines
57 KiB
HTML
597 lines
57 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>os.path --- 常用路径操作 — 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="fileinput --- 迭代来自多个输入流的行" href="fileinput.html" />
|
||
<link rel="prev" title="pathlib --- 面向对象的文件系统路径" href="pathlib.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/os.path.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>
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="pathlib.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code> --- 面向对象的文件系统路径</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="fileinput.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fileinput</span></code> --- 迭代来自多个输入流的行</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/os.path.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/os.path.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="fileinput.html" title="fileinput --- 迭代来自多个输入流的行"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="pathlib.html" title="pathlib --- 面向对象的文件系统路径"
|
||
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-os.path">
|
||
<span id="os-path-common-pathname-manipulations"></span><h1><a class="reference internal" href="#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a> --- 常用路径操作<a class="headerlink" href="#module-os.path" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/posixpath.py">Lib/posixpath.py</a> (用于 POSIX)和 <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/ntpath.py">Lib/ntpath.py</a> (用于 Windows NT)</p>
|
||
<hr class="docutils" id="index-0" />
|
||
<p>该模块在路径名上实现了一些有用的功能:如需读取或写入文件,请参见 <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</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> 模块。路径参数可以字符串或字节形式传递。我们鼓励应用程序将文件名表示为(Unicode)字符串。不幸的是,某些文件名在Unix上可能无法用字符串表示,因此在Unix上平台上需要支持任意文件名的应用程序,应使用字节对象来表示路径名。反之亦然,在Windows平台上仅使用字节对象,不能表示的所有文件名(以标准 <code class="docutils literal notranslate"><span class="pre">mbcs</span></code> 编码),因此Windows应用程序应使用字符串对象来访问所有文件。</p>
|
||
<p>与unix shell不同,Python不执行任何 <em>自动</em> 路径扩展。当应用程序需要类似shell的路径扩展时,可以显式调用诸如 <a class="reference internal" href="#os.path.expanduser" title="os.path.expanduser"><code class="xref py py-func docutils literal notranslate"><span class="pre">expanduser()</span></code></a> 和 <a class="reference internal" href="#os.path.expandvars" title="os.path.expandvars"><code class="xref py py-func docutils literal notranslate"><span class="pre">expandvars()</span></code></a> 之类的函数。 (另请参见 <a class="reference internal" href="glob.html#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code></a> 模块。)</p>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<p><a class="reference internal" href="pathlib.html#module-pathlib" title="pathlib: Object-oriented filesystem paths"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code></a> 模块提供高级路径对象。</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>所有这些函数都仅接受字节或字符串对象作为其参数。如果返回路径或文件名,则结果是相同类型的对象。</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>由于不同的操作系统具有不同的路径名称约定,因此标准库中有此模块的几个版本。<a class="reference internal" href="#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a> 模块始终是适合 Python 运行的操作系统的路径模块,因此可用于本地路径。但是,如果操作的路径 <em>总是</em> 以一种不同的格式显示,那么也可以分别导入和使用各个模块。它们都具有相同的接口:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">posixpath</span></code> 用于Unix 样式的路径</p></li>
|
||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">ntpath</span></code> 用于 Windows 路径</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span><a class="reference internal" href="#os.path.exists" title="os.path.exists"><code class="xref py py-func docutils literal notranslate"><span class="pre">exists()</span></code></a>、<a class="reference internal" href="#os.path.lexists" title="os.path.lexists"><code class="xref py py-func docutils literal notranslate"><span class="pre">lexists()</span></code></a>、<a class="reference internal" href="#os.path.isdir" title="os.path.isdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">isdir()</span></code></a>、<a class="reference internal" href="#os.path.isfile" title="os.path.isfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfile()</span></code></a>、<a class="reference internal" href="#os.path.islink" title="os.path.islink"><code class="xref py py-func docutils literal notranslate"><span class="pre">islink()</span></code></a> 和 <a class="reference internal" href="#os.path.ismount" title="os.path.ismount"><code class="xref py py-func docutils literal notranslate"><span class="pre">ismount()</span></code></a> 现在遇到系统层面上不可表示的字符或字节的路径时,会返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>,而不是抛出异常。</p>
|
||
</div>
|
||
<dl class="function">
|
||
<dt id="os.path.abspath">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">abspath</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.abspath" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回路径 <em>path</em> 的绝对路径(标准化的)。在大多数平台上,这等同于用 <code class="docutils literal notranslate"><span class="pre">normpath(join(os.getcwd(),</span> <span class="pre">path))</span></code> 的方式调用 <a class="reference internal" href="#os.path.normpath" title="os.path.normpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">normpath()</span></code></a> 函数。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.basename">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">basename</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.basename" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回路径 <em>path</em> 的基本名称。这是将 <em>path</em> 传入函数 <a class="reference internal" href="#os.path.split" title="os.path.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">split()</span></code></a> 之后,返回的一对值中的第二个元素。请注意,此函数的结果与Unix <strong class="program">basename</strong> 程序不同。<strong class="program">basename</strong> 在 <code class="docutils literal notranslate"><span class="pre">'/foo/bar/'</span></code> 上返回 <code class="docutils literal notranslate"><span class="pre">'bar'</span></code>,而 <a class="reference internal" href="#os.path.basename" title="os.path.basename"><code class="xref py py-func docutils literal notranslate"><span class="pre">basename()</span></code></a> 函数返回一个空字符串 (<code class="docutils literal notranslate"><span class="pre">''</span></code>)。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.commonpath">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">commonpath</code><span class="sig-paren">(</span><em class="sig-param">paths</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.commonpath" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>接受包含多个路径的序列 <em>paths</em>,返回 <em>paths</em> 的最长公共子路径。如果 <em>paths</em> 同时包含绝对路径和相对路径,或 <em>paths</em> 在不同的驱动器上,或 <em>paths</em> 为空,则抛出 <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> 异常。与 <a class="reference internal" href="#os.path.commonprefix" title="os.path.commonprefix"><code class="xref py py-func docutils literal notranslate"><span class="pre">commonprefix()</span></code></a> 不同,本方法返回有效路径。</p>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: Unix, Windows。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.5 新版功能.</span></p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">类路径对象</span></a> 序列。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.commonprefix">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">commonprefix</code><span class="sig-paren">(</span><em class="sig-param">list</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.commonprefix" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>接受包含多个路径的 <em>列表</em>,返回所有路径的最长公共前缀(逐字符比较)。如果 <em>列表</em> 为空,则返回空字符串 (<code class="docutils literal notranslate"><span class="pre">''</span></code>)。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>此函数是逐字符比较,因此可能返回无效路径。要获取有效路径,参见 <a class="reference internal" href="#os.path.commonpath" title="os.path.commonpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">commonpath()</span></code></a>。</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">commonprefix</span><span class="p">([</span><span class="s1">'/usr/lib'</span><span class="p">,</span> <span class="s1">'/usr/local/lib'</span><span class="p">])</span>
|
||
<span class="go">'/usr/l'</span>
|
||
|
||
<span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">commonpath</span><span class="p">([</span><span class="s1">'/usr/lib'</span><span class="p">,</span> <span class="s1">'/usr/local/lib'</span><span class="p">])</span>
|
||
<span class="go">'/usr'</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.dirname">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">dirname</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.dirname" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回路径 <em>path</em> 的目录名称。这是将 <em>path</em> 传入函数 <a class="reference internal" href="#os.path.split" title="os.path.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">split()</span></code></a> 之后,返回的一对值中的第一个元素。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.exists">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">exists</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.exists" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>path</em> 指向一个已存在的路径或已打开的文件描述符,返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。对于失效的符号链接,返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。在某些平台上,如果使用 <a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> 查询到目标文件没有执行权限,即使 <em>path</em> 确实存在,本函数也可能返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span><em>path</em> 现在可以是一个整数:如果该整数是一个已打开的文件描述符,返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>,否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.lexists">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">lexists</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.lexists" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>path</em> 指向一个已存在的路径,返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。对于失效的符号链接,也返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。在缺失 <a class="reference internal" href="os.html#os.lstat" title="os.lstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.lstat()</span></code></a> 的平台上等同于 <a class="reference internal" href="#os.path.exists" title="os.path.exists"><code class="xref py py-func docutils literal notranslate"><span class="pre">exists()</span></code></a>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<span class="target" id="index-1"></span><dl class="function">
|
||
<dt id="os.path.expanduser">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">expanduser</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.expanduser" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在 Unix 和 Windows 上,将参数中开头部分的 <code class="docutils literal notranslate"><span class="pre">~</span></code> 或 <code class="docutils literal notranslate"><span class="pre">~user</span></code> 替换为当前 <em>用户</em> 的家目录并返回。</p>
|
||
<p id="index-2">在 Unix 上,开头的 <code class="docutils literal notranslate"><span class="pre">~</span></code> 会被环境变量 <span class="target" id="index-14"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">HOME</span></code> 代替,如果变量未设置,则通过内置模块 <a class="reference internal" href="pwd.html#module-pwd" title="pwd: The password database (getpwnam() and friends). (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pwd</span></code></a> 在 password 目录中查找当前用户的主目录。以 <code class="docutils literal notranslate"><span class="pre">~user</span></code> 开头则直接在 password 目录中查找。</p>
|
||
<p>在 Windows 上,如果设置了 <span class="target" id="index-15"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">USERPROFILE</span></code>,就使用这个变量,否则会将 <span class="target" id="index-16"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">HOMEPATH</span></code> 和 <span class="target" id="index-17"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">HOMEDRIVE</span></code> 结合在一起使用。以 <code class="docutils literal notranslate"><span class="pre">~user</span></code> 开头则将上述方法生成路径的最后一截目录替换成 user。</p>
|
||
<p>如果展开路径失败,或者路径不是以波浪号开头,则路径将保持不变。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>Windows 不再使用 <span class="target" id="index-18"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">HOME</span></code>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<span class="target" id="index-8"></span><dl class="function">
|
||
<dt id="os.path.expandvars">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">expandvars</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.expandvars" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>输入带有环境变量的路径作为参数,返回展开变量以后的路径。<code class="docutils literal notranslate"><span class="pre">$name</span></code> 或 <code class="docutils literal notranslate"><span class="pre">${name}</span></code> 形式的子字符串被环境变量 <em>name</em> 的值替换。格式错误的变量名称和对不存在变量的引用保持不变。</p>
|
||
<p>在 Windows 上,除了 <code class="docutils literal notranslate"><span class="pre">$name</span></code> 和 <code class="docutils literal notranslate"><span class="pre">${name}</span></code> 外,还可以展开 <code class="docutils literal notranslate"><span class="pre">%name%</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.getatime">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">getatime</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.getatime" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回 <em>path</em> 的最后访问时间。返回值是一个浮点数,为纪元秒数(参见 <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</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> 异常。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.getmtime">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">getmtime</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.getmtime" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回 <em>path</em> 的最后修改时间。返回值是一个浮点数,为纪元秒数(参见 <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</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> 异常。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.getctime">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">getctime</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.getctime" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回 <em>path</em> 在系统中的 ctime,在有些系统(比如 Unix)上,它是元数据的最后修改时间,其他系统(比如 Windows)上,它是 <em>path</em> 的创建时间。返回值是一个数,为纪元秒数(参见 <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</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> 异常。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.getsize">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">getsize</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.getsize" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回 <em>path</em> 的大小,以字节为单位。如果该文件不存在或不可访问,则抛出 <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> 异常。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.isabs">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">isabs</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.isabs" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>path</em> 是一个绝对路径,则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。在 Unix 上,它就是以斜杠开头,而在 Windows 上,它可以是去掉驱动器号后以斜杠(或反斜杠)开头。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.isfile">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">isfile</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.isfile" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>path</em> 是 <a class="reference internal" href="#os.path.exists" title="os.path.exists"><code class="xref py py-func docutils literal notranslate"><span class="pre">现有的</span></code></a> 常规文件,则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。本方法会跟踪符号链接,因此,对于同一路径,<a class="reference internal" href="#os.path.islink" title="os.path.islink"><code class="xref py py-func docutils literal notranslate"><span class="pre">islink()</span></code></a> 和 <a class="reference internal" href="#os.path.isfile" title="os.path.isfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfile()</span></code></a> 都可能为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.isdir">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">isdir</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.isdir" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>path</em> 是 <a class="reference internal" href="#os.path.exists" title="os.path.exists"><code class="xref py py-func docutils literal notranslate"><span class="pre">现有的</span></code></a> 目录,则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。本方法会跟踪符号链接,因此,对于同一路径,<a class="reference internal" href="#os.path.islink" title="os.path.islink"><code class="xref py py-func docutils literal notranslate"><span class="pre">islink()</span></code></a> 和 <a class="reference internal" href="#os.path.isdir" title="os.path.isdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">isdir()</span></code></a> 都可能为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.islink">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">islink</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.islink" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 <em>path</em> 指向的 <a class="reference internal" href="#os.path.exists" title="os.path.exists"><code class="xref py py-func docutils literal notranslate"><span class="pre">现有</span></code></a> 目录条目是一个符号链接,则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。如果 Python 运行时不支持符号链接,则总是返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.ismount">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">ismount</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.ismount" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果路径 <em>path</em> 是 <em class="dfn">挂载点</em> (文件系统中挂载其他文件系统的点),则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。在 POSIX 上,该函数检查 <em>path</em> 的父目录 <code class="file docutils literal notranslate"><em><span class="pre">path</span></em><span class="pre">/..</span></code> 是否在与 <em>path</em> 不同的设备上,或者 <code class="file docutils literal notranslate"><em><span class="pre">path</span></em><span class="pre">/..</span></code> 和 <em>path</em> 是否指向同一设备上的同一 inode(这一检测挂载点的方法适用于所有 Unix 和 POSIX 变体)。本方法不能可靠地检测同一文件系统上的绑定挂载 (bind mount)。在 Windows 上,盘符和共享 UNC 始终是挂载点,对于任何其他路径,将调用 <code class="docutils literal notranslate"><span class="pre">GetVolumePathName</span></code> 来查看它是否与输入的路径不同。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.4 新版功能: </span>支持在 Windows 上检测非根挂载点。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.join">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">join</code><span class="sig-paren">(</span><em class="sig-param">path</em>, <em class="sig-param">*paths</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.join" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>智能地拼接一个或多个路径部分。 返回值是 <em>path</em> 和 <em>*paths</em> 的所有成员的拼接,其中每个非空部分后面都紧跟一个目录分隔符,最后一个部分除外,这意味着如果最后一个部分为空,则结果将以分隔符结尾。 如果某个部分为绝对路径,则之前的所有部分会被丢弃并从绝对路径部分开始继续拼接。</p>
|
||
<p>在 Windows 上,遇到绝对路径部分(例如 <code class="docutils literal notranslate"><span class="pre">r'\foo'</span></code>)时,不会重置盘符。如果某部分路径包含盘符,则会丢弃所有先前的部分,并重置盘符。请注意,由于每个驱动器都有一个“当前目录”,所以 <code class="docutils literal notranslate"><span class="pre">os.path.join("c:",</span> <span class="pre">"foo")</span></code> 表示驱动器 <code class="file docutils literal notranslate"><span class="pre">C:</span></code> 上当前目录的相对路径 (<code class="file docutils literal notranslate"><span class="pre">c:foo</span></code>),而不是 <code class="file docutils literal notranslate"><span class="pre">c:\foo</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">类路径对象</span></a> 用于 <em>path</em> 和 <em>paths</em> 。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.normcase">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">normcase</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.normcase" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>规范路径的大小写。在 Windows 上,将路径中的所有字符都转换为小写,并将正斜杠转换为反斜杠。在其他操作系统上返回原路径。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.normpath">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">normpath</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.normpath" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>通过折叠多余的分隔符和对上级目录的引用来标准化路径名,所以 <code class="docutils literal notranslate"><span class="pre">A//B</span></code>、<code class="docutils literal notranslate"><span class="pre">A/B/</span></code>、<code class="docutils literal notranslate"><span class="pre">A/./B</span></code> 和 <code class="docutils literal notranslate"><span class="pre">A/foo/../B</span></code> 都会转换成 <code class="docutils literal notranslate"><span class="pre">A/B</span></code>。这个字符串操作可能会改变带有符号链接的路径的含义。在 Windows 上,本方法将正斜杠转换为反斜杠。要规范大小写,请使用 <a class="reference internal" href="#os.path.normcase" title="os.path.normcase"><code class="xref py py-func docutils literal notranslate"><span class="pre">normcase()</span></code></a>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.realpath">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">realpath</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.realpath" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回指定文件的规范路径,消除路径中存在的任何符号链接(如果操作系统支持)。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>当发生符号链接循环时,返回的路径将是该循环的某个组成部分,但不能保证是哪个部分。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span>在 Windows 上现在可以正确解析符号链接和交接点 (junction point)。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.relpath">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">relpath</code><span class="sig-paren">(</span><em class="sig-param">path</em>, <em class="sig-param">start=os.curdir</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.relpath" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回从当前目录或可选的 <em>start</em> 目录至 <em>path</em> 的相对文件路径。 这只是一个路径计算:不会访问文件系统来确认 <em>path</em> 或 <em>start</em> 是否存在或其性质。 在 Windows 上,当 <em>path</em> 和 <em>start</em> 位于不同驱动器时将引发 <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><em>start</em> 默认为 <a class="reference internal" href="os.html#os.curdir" title="os.curdir"><code class="xref py py-attr docutils literal notranslate"><span class="pre">os.curdir</span></code></a>。</p>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: Unix, Windows。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.samefile">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">samefile</code><span class="sig-paren">(</span><em class="sig-param">path1</em>, <em class="sig-param">path2</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.samefile" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果两个路径都指向相同的文件或目录,则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。这由设备号和 inode 号确定,在任一路径上调用 <a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> 失败则抛出异常。</p>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: Unix, Windows。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了对 Windows 的支持。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.4 版更改: </span>Windows现在使用与其他所有平台相同的实现。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.sameopenfile">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">sameopenfile</code><span class="sig-paren">(</span><em class="sig-param">fp1</em>, <em class="sig-param">fp2</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.sameopenfile" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果文件描述符 <em>fp1</em> 和 <em>fp2</em> 指向相同文件,则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: Unix, Windows。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了对 Windows 的支持。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.samestat">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">samestat</code><span class="sig-paren">(</span><em class="sig-param">stat1</em>, <em class="sig-param">stat2</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.samestat" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 stat 元组 <em>stat1</em> 和 <em>stat2</em> 指向相同文件,则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。这些 stat 元组可能是由 <a class="reference internal" href="os.html#os.fstat" title="os.fstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fstat()</span></code></a>、<a class="reference internal" href="os.html#os.lstat" title="os.lstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.lstat()</span></code></a> 或 <a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> 返回的。本函数实现了 <a class="reference internal" href="#os.path.samefile" title="os.path.samefile"><code class="xref py py-func docutils literal notranslate"><span class="pre">samefile()</span></code></a> 和 <a class="reference internal" href="#os.path.sameopenfile" title="os.path.sameopenfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sameopenfile()</span></code></a> 底层所使用的比较过程。</p>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">可用性</span></a>: Unix, Windows。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.4 版更改: </span>添加了对 Windows 的支持。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.split">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">split</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.split" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将路径 <em>path</em> 拆分为一对,即 <code class="docutils literal notranslate"><span class="pre">(head,</span> <span class="pre">tail)</span></code>,其中,<em>tail</em> 是路径的最后一部分,而 <em>head</em> 里是除最后部分外的所有内容。<em>tail</em> 部分不会包含斜杠,如果 <em>path</em> 以斜杠结尾,则 <em>tail</em> 将为空。如果 <em>path</em> 中没有斜杠,<em>head</em> 将为空。如果 <em>path</em> 为空,则 <em>head</em> 和 <em>tail</em> 均为空。<em>head</em> 末尾的斜杠会被去掉,除非它是根目录(即它仅包含一个或多个斜杠)。在所有情况下,<code class="docutils literal notranslate"><span class="pre">join(head,</span> <span class="pre">tail)</span></code> 指向的位置都与 <em>path</em> 相同(但字符串可能不同)。另请参见函数 <a class="reference internal" href="#os.path.dirname" title="os.path.dirname"><code class="xref py py-func docutils literal notranslate"><span class="pre">dirname()</span></code></a> 和 <a class="reference internal" href="#os.path.basename" title="os.path.basename"><code class="xref py py-func docutils literal notranslate"><span class="pre">basename()</span></code></a>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.splitdrive">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">splitdrive</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.splitdrive" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将路径 <em>path</em> 拆分为一对,即 <code class="docutils literal notranslate"><span class="pre">(drive,</span> <span class="pre">tail)</span></code>,其中 <em>drive</em> 是挂载点或空字符串。在没有驱动器概念的系统上,<em>drive</em> 将始终为空字符串。在所有情况下,<code class="docutils literal notranslate"><span class="pre">drive</span> <span class="pre">+</span> <span class="pre">tail</span></code> 都与 <em>path</em> 相同。</p>
|
||
<p>在 Windows 上,本方法将路径拆分为驱动器/UNC 根节点和相对路径。</p>
|
||
<p>如果路径 path 包含盘符,则 drive 将包含冒号及冒号前面的所有内容。例如 <code class="docutils literal notranslate"><span class="pre">splitdrive("c:/dir")</span></code> 返回 <code class="docutils literal notranslate"><span class="pre">("c:",</span> <span class="pre">"/dir")</span></code>。</p>
|
||
<p>如果 path 是一个 UNC 路径,则 drive 将包含主机名和共享点,但不包括第四个分隔符。例如 <code class="docutils literal notranslate"><span class="pre">splitdrive("//host/computer/dir")</span></code> 返回 <code class="docutils literal notranslate"><span class="pre">("//host/computer",</span> <span class="pre">"/dir")</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="os.path.splitext">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">splitext</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#os.path.splitext" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将路径 <em>path</em> 拆分为一对,即 <code class="docutils literal notranslate"><span class="pre">(root,</span> <span class="pre">ext)</span></code>,使 <code class="docutils literal notranslate"><span class="pre">root</span> <span class="pre">+</span> <span class="pre">ext</span> <span class="pre">==</span> <span class="pre">path</span></code>,其中 <em>ext</em> 为空或以英文句点开头,且最多包含一个句点。路径前的句点将被忽略,例如 <code class="docutils literal notranslate"><span class="pre">splitext('.cshrc')</span></code> 返回 <code class="docutils literal notranslate"><span class="pre">('.cshrc',</span> <span class="pre">'')</span></code>。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="os.path.supports_unicode_filenames">
|
||
<code class="sig-prename descclassname">os.path.</code><code class="sig-name descname">supports_unicode_filenames</code><a class="headerlink" href="#os.path.supports_unicode_filenames" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果(在文件系统限制下)允许将任意 Unicode 字符串用作文件名,则为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="pathlib.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code> --- 面向对象的文件系统路径</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="fileinput.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fileinput</span></code> --- 迭代来自多个输入流的行</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/os.path.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="fileinput.html" title="fileinput --- 迭代来自多个输入流的行"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="pathlib.html" title="pathlib --- 面向对象的文件系统路径"
|
||
>上一页</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> |