Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
pathlib.html2537 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="pathlib — Object-oriented filesystem paths" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/pathlib.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/pathlib/ This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which pro..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_pathlib_12bf8fbf.png" />15<meta property="og:image:alt" content="Source code: Lib/pathlib/ This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which pro..." />16<meta name="description" content="Source code: Lib/pathlib/ This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which pro..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>pathlib — Object-oriented filesystem paths &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="os.path — Common pathname manipulations" href="os.path.html" />43    <link rel="prev" title="File and Directory Access" href="filesys.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/pathlib.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h3><a href="../contents.html">Table of Contents</a></h3>106    <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code> — Object-oriented filesystem paths</a><ul>108<li><a class="reference internal" href="#basic-use">Basic use</a></li>109<li><a class="reference internal" href="#exceptions">Exceptions</a></li>110<li><a class="reference internal" href="#pure-paths">Pure paths</a><ul>111<li><a class="reference internal" href="#general-properties">General properties</a></li>112<li><a class="reference internal" href="#operators">Operators</a></li>113<li><a class="reference internal" href="#accessing-individual-parts">Accessing individual parts</a></li>114<li><a class="reference internal" href="#methods-and-properties">Methods and properties</a></li>115</ul>116</li>117<li><a class="reference internal" href="#concrete-paths">Concrete paths</a><ul>118<li><a class="reference internal" href="#parsing-and-generating-uris">Parsing and generating URIs</a></li>119<li><a class="reference internal" href="#expanding-and-resolving-paths">Expanding and resolving paths</a></li>120<li><a class="reference internal" href="#querying-file-type-and-status">Querying file type and status</a></li>121<li><a class="reference internal" href="#reading-and-writing-files">Reading and writing files</a></li>122<li><a class="reference internal" href="#reading-directories">Reading directories</a></li>123<li><a class="reference internal" href="#creating-files-and-directories">Creating files and directories</a></li>124<li><a class="reference internal" href="#copying-moving-and-deleting">Copying, moving and deleting</a></li>125<li><a class="reference internal" href="#permissions-and-ownership">Permissions and ownership</a></li>126</ul>127</li>128<li><a class="reference internal" href="#pattern-language">Pattern language</a></li>129<li><a class="reference internal" href="#comparison-to-the-glob-module">Comparison to the <code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> module</a></li>130<li><a class="reference internal" href="#comparison-to-the-os-and-os-path-modules">Comparison to the <code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code> and <code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code> modules</a><ul>131<li><a class="reference internal" href="#corresponding-tools">Corresponding tools</a></li>132</ul>133</li>134<li><a class="reference internal" href="#module-pathlib.types">Protocols</a></li>135</ul>136</li>137</ul>138 139  </div>140  <div>141    <h4>Previous topic</h4>142    <p class="topless"><a href="filesys.html"143                          title="previous chapter">File and Directory Access</a></p>144  </div>145  <div>146    <h4>Next topic</h4>147    <p class="topless"><a href="os.path.html"148                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code> — Common pathname manipulations</a></p>149  </div>150  <script>151    document.addEventListener('DOMContentLoaded', () => {152        const title = document.querySelector('meta[property="og:title"]').content;153        const elements = document.querySelectorAll('.improvepage');154        const pageurl = window.location.href.split('?')[0];155        elements.forEach(element => {156            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));157            url.searchParams.set('pagetitle', title);158            url.searchParams.set('pageurl', pageurl);159            url.searchParams.set('pagesource', "library/pathlib.rst");160            element.href = url.toString();161        });162    });163  </script>164  <div role="note" aria-label="source link">165    <h3>This page</h3>166    <ul class="this-page-menu">167      <li><a href="../bugs.html">Report a bug</a></li>168      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>169      <li>170        <a href="https://github.com/python/cpython/blob/main/Doc/library/pathlib.rst?plain=1"171            rel="nofollow">Show source172        </a>173      </li>174      175    </ul>176  </div>177        </nav>178    </div>179</div>180 181  182    <div class="related" role="navigation" aria-label="Related">183      <h3>Navigation</h3>184      <ul>185        <li class="right" style="margin-right: 10px">186          <a href="../genindex.html" title="General Index"187             accesskey="I">index</a></li>188        <li class="right" >189          <a href="../py-modindex.html" title="Python Module Index"190             >modules</a> |</li>191        <li class="right" >192          <a href="os.path.html" title="os.path — Common pathname manipulations"193             accesskey="N">next</a> |</li>194        <li class="right" >195          <a href="filesys.html" title="File and Directory Access"196             accesskey="P">previous</a> |</li>197 198          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>199          <li><a href="https://www.python.org/">Python</a> &#187;</li>200          <li class="switchers">201            <div class="language_switcher_placeholder"></div>202            <div class="version_switcher_placeholder"></div>203          </li>204          <li>205              206          </li>207    <li id="cpython-language-and-version">208      <a href="../index.html">3.15.0a6 Documentation</a> &#187;209    </li>210 211          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>212          <li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">File and Directory Access</a> &#187;</li>213        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code> — Object-oriented filesystem paths</a></li>214                <li class="right">215                    216 217    <div class="inline-search" role="search">218        <form class="inline-search" action="../search.html" method="get">219          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">220          <input type="submit" value="Go">221        </form>222    </div>223                     |224                </li>225            <li class="right">226<label class="theme-selector-label">227    Theme228    <select class="theme-selector" oninput="activateTheme(this.value)">229        <option value="auto" selected>Auto</option>230        <option value="light">Light</option>231        <option value="dark">Dark</option>232    </select>233</label> |</li>234            235      </ul>236    </div>    237 238    <div class="document">239      <div class="documentwrapper">240        <div class="bodywrapper">241          <div class="body" role="main">242            243  <section id="module-pathlib">244<span id="pathlib-object-oriented-filesystem-paths"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code> — Object-oriented filesystem paths<a class="headerlink" href="#module-pathlib" title="Link to this heading">¶</a></h1>245<div class="versionadded">246<p><span class="versionmodified added">Added in version 3.4.</span></p>247</div>248<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/pathlib/">Lib/pathlib/</a></p>249<hr class="docutils" id="index-0" />250<p>This module offers classes representing filesystem paths with semantics251appropriate for different operating systems.  Path classes are divided252between <a class="reference internal" href="#pure-paths"><span class="std std-ref">pure paths</span></a>, which provide purely computational253operations without I/O, and <a class="reference internal" href="#concrete-paths"><span class="std std-ref">concrete paths</span></a>, which254inherit from pure paths but also provide I/O operations.</p>255<img alt="Inheritance diagram showing the classes available in pathlib. The most basic class is PurePath, which has three direct subclasses: PurePosixPath, PureWindowsPath, and Path. Further to these four classes, there are two classes that use multiple inheritance: PosixPath subclasses PurePosixPath and Path, and WindowsPath subclasses PureWindowsPath and Path." class="invert-in-dark-mode align-center" src="../_images/pathlib-inheritance.png" />256<p>If you’ve never used this module before or just aren’t sure which class is257right for your task, <a class="reference internal" href="#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code></a> is most likely what you need. It instantiates258a <a class="reference internal" href="#concrete-paths"><span class="std std-ref">concrete path</span></a> for the platform the code is running on.</p>259<p>Pure paths are useful in some special cases; for example:</p>260<ol class="arabic simple">261<li><p>If you want to manipulate Windows paths on a Unix machine (or vice versa).262You cannot instantiate a <a class="reference internal" href="#pathlib.WindowsPath" title="pathlib.WindowsPath"><code class="xref py py-class docutils literal notranslate"><span class="pre">WindowsPath</span></code></a> when running on Unix, but you263can instantiate <a class="reference internal" href="#pathlib.PureWindowsPath" title="pathlib.PureWindowsPath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PureWindowsPath</span></code></a>.</p></li>264<li><p>You want to make sure that your code only manipulates paths without actually265accessing the OS. In this case, instantiating one of the pure classes may be266useful since those simply don’t have any OS-accessing operations.</p></li>267</ol>268<div class="admonition seealso">269<p class="admonition-title">See also</p>270<p><span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0428/"><strong>PEP 428</strong></a>: The pathlib module – object-oriented filesystem paths.</p>271</div>272<div class="admonition seealso">273<p class="admonition-title">See also</p>274<p>For low-level path manipulation on strings, you can also use the275<a class="reference internal" href="os.path.html#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> module.</p>276</div>277<section id="basic-use">278<h2>Basic use<a class="headerlink" href="#basic-use" title="Link to this heading">¶</a></h2>279<p>Importing the main class:</p>280<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span><span class="w"> </span><span class="nn">pathlib</span><span class="w"> </span><span class="kn">import</span> <span class="n">Path</span>281</pre></div>282</div>283<p>Listing subdirectories:</p>284<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>285<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">p</span><span class="o">.</span><span class="n">iterdir</span><span class="p">()</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">is_dir</span><span class="p">()]</span>286<span class="go">[PosixPath(&#39;.hg&#39;), PosixPath(&#39;docs&#39;), PosixPath(&#39;dist&#39;),</span>287<span class="go"> PosixPath(&#39;__pycache__&#39;), PosixPath(&#39;build&#39;)]</span>288</pre></div>289</div>290<p>Listing Python source files in this directory tree:</p>291<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;**/*.py&#39;</span><span class="p">))</span>292<span class="go">[PosixPath(&#39;test_pathlib.py&#39;), PosixPath(&#39;setup.py&#39;),</span>293<span class="go"> PosixPath(&#39;pathlib.py&#39;), PosixPath(&#39;docs/conf.py&#39;),</span>294<span class="go"> PosixPath(&#39;build/lib/pathlib.py&#39;)]</span>295</pre></div>296</div>297<p>Navigating inside a directory tree:</p>298<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span>299<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">p</span> <span class="o">/</span> <span class="s1">&#39;init.d&#39;</span> <span class="o">/</span> <span class="s1">&#39;reboot&#39;</span>300<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span>301<span class="go">PosixPath(&#39;/etc/init.d/reboot&#39;)</span>302<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="o">.</span><span class="n">resolve</span><span class="p">()</span>303<span class="go">PosixPath(&#39;/etc/rc.d/init.d/halt&#39;)</span>304</pre></div>305</div>306<p>Querying path properties:</p>307<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>308<span class="go">True</span>309<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span><span class="o">.</span><span class="n">is_dir</span><span class="p">()</span>310<span class="go">False</span>311</pre></div>312</div>313<p>Opening a file:</p>314<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">q</span><span class="o">.</span><span class="n">open</span><span class="p">()</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span> <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>315<span class="gp">...</span>316<span class="go">&#39;#!/bin/bash\n&#39;</span>317</pre></div>318</div>319</section>320<section id="exceptions">321<h2>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h2>322<dl class="py exception">323<dt class="sig sig-object py" id="pathlib.UnsupportedOperation">324<em class="property"><span class="k"><span class="pre">exception</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pathlib.</span></span><span class="sig-name descname"><span class="pre">UnsupportedOperation</span></span><a class="headerlink" href="#pathlib.UnsupportedOperation" title="Link to this definition">¶</a></dt>325<dd><p>An exception inheriting <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> that is raised when an326unsupported operation is called on a path object.</p>327<div class="versionadded">328<p><span class="versionmodified added">Added in version 3.13.</span></p>329</div>330</dd></dl>331 332</section>333<section id="pure-paths">334<span id="id1"></span><h2>Pure paths<a class="headerlink" href="#pure-paths" title="Link to this heading">¶</a></h2>335<p>Pure path objects provide path-handling operations which don’t actually336access a filesystem.  There are three ways to access these classes, which337we also call <em>flavours</em>:</p>338<dl class="py class">339<dt class="sig sig-object py" id="pathlib.PurePath">340<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pathlib.</span></span><span class="sig-name descname"><span class="pre">PurePath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">pathsegments</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath" title="Link to this definition">¶</a></dt>341<dd><p>A generic class that represents the system’s path flavour (instantiating342it creates either a <a class="reference internal" href="#pathlib.PurePosixPath" title="pathlib.PurePosixPath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePosixPath</span></code></a> or a <a class="reference internal" href="#pathlib.PureWindowsPath" title="pathlib.PureWindowsPath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PureWindowsPath</span></code></a>):</p>343<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;setup.py&#39;</span><span class="p">)</span>      <span class="c1"># Running on a Unix machine</span>344<span class="go">PurePosixPath(&#39;setup.py&#39;)</span>345</pre></div>346</div>347<p>Each element of <em>pathsegments</em> can be either a string representing a348path segment, or an object implementing the <a class="reference internal" href="os.html#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.PathLike</span></code></a> interface349where the <a class="reference internal" href="os.html#os.PathLike.__fspath__" title="os.PathLike.__fspath__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__fspath__()</span></code></a> method returns a string,350such as another path object:</p>351<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;some/path&#39;</span><span class="p">,</span> <span class="s1">&#39;bar&#39;</span><span class="p">)</span>352<span class="go">PurePosixPath(&#39;foo/some/path/bar&#39;)</span>353<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">),</span> <span class="n">Path</span><span class="p">(</span><span class="s1">&#39;bar&#39;</span><span class="p">))</span>354<span class="go">PurePosixPath(&#39;foo/bar&#39;)</span>355</pre></div>356</div>357<p>When <em>pathsegments</em> is empty, the current directory is assumed:</p>358<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">()</span>359<span class="go">PurePosixPath(&#39;.&#39;)</span>360</pre></div>361</div>362<p>If a segment is an absolute path, all previous segments are ignored363(like <a class="reference internal" href="os.path.html#os.path.join" title="os.path.join"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.join()</span></code></a>):</p>364<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">,</span> <span class="s1">&#39;/usr&#39;</span><span class="p">,</span> <span class="s1">&#39;lib64&#39;</span><span class="p">)</span>365<span class="go">PurePosixPath(&#39;/usr/lib64&#39;)</span>366<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Windows&#39;</span><span class="p">,</span> <span class="s1">&#39;d:bar&#39;</span><span class="p">)</span>367<span class="go">PureWindowsPath(&#39;d:bar&#39;)</span>368</pre></div>369</div>370<p>On Windows, the drive is not reset when a rooted relative path371segment (e.g., <code class="docutils literal notranslate"><span class="pre">r'\foo'</span></code>) is encountered:</p>372<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Windows&#39;</span><span class="p">,</span> <span class="s1">&#39;/Program Files&#39;</span><span class="p">)</span>373<span class="go">PureWindowsPath(&#39;c:/Program Files&#39;)</span>374</pre></div>375</div>376<p>Spurious slashes and single dots are collapsed, but double dots (<code class="docutils literal notranslate"><span class="pre">'..'</span></code>)377and leading double slashes (<code class="docutils literal notranslate"><span class="pre">'//'</span></code>) are not, since this would change the378meaning of a path for various reasons (e.g. symbolic links, UNC paths):</p>379<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;foo//bar&#39;</span><span class="p">)</span>380<span class="go">PurePosixPath(&#39;foo/bar&#39;)</span>381<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;//foo/bar&#39;</span><span class="p">)</span>382<span class="go">PurePosixPath(&#39;//foo/bar&#39;)</span>383<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;foo/./bar&#39;</span><span class="p">)</span>384<span class="go">PurePosixPath(&#39;foo/bar&#39;)</span>385<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;foo/../bar&#39;</span><span class="p">)</span>386<span class="go">PurePosixPath(&#39;foo/../bar&#39;)</span>387</pre></div>388</div>389<p>(a naïve approach would make <code class="docutils literal notranslate"><span class="pre">PurePosixPath('foo/../bar')</span></code> equivalent390to <code class="docutils literal notranslate"><span class="pre">PurePosixPath('bar')</span></code>, which is wrong if <code class="docutils literal notranslate"><span class="pre">foo</span></code> is a symbolic link391to another directory)</p>392<p>Pure path objects implement the <a class="reference internal" href="os.html#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.PathLike</span></code></a> interface, allowing them393to be used anywhere the interface is accepted.</p>394<div class="versionchanged">395<p><span class="versionmodified changed">Changed in version 3.6: </span>Added support for the <a class="reference internal" href="os.html#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.PathLike</span></code></a> interface.</p>396</div>397</dd></dl>398 399<dl class="py class">400<dt class="sig sig-object py" id="pathlib.PurePosixPath">401<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pathlib.</span></span><span class="sig-name descname"><span class="pre">PurePosixPath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">pathsegments</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePosixPath" title="Link to this definition">¶</a></dt>402<dd><p>A subclass of <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a>, this path flavour represents non-Windows403filesystem paths:</p>404<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/etc/hosts&#39;</span><span class="p">)</span>405<span class="go">PurePosixPath(&#39;/etc/hosts&#39;)</span>406</pre></div>407</div>408<p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a>.</p>409</dd></dl>410 411<dl class="py class">412<dt class="sig sig-object py" id="pathlib.PureWindowsPath">413<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pathlib.</span></span><span class="sig-name descname"><span class="pre">PureWindowsPath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">pathsegments</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PureWindowsPath" title="Link to this definition">¶</a></dt>414<dd><p>A subclass of <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a>, this path flavour represents Windows415filesystem paths, including <a class="reference external" href="https://en.wikipedia.org/wiki/Path_(computing)#UNC">UNC paths</a>:</p>416<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/&#39;</span><span class="p">,</span> <span class="s1">&#39;Users&#39;</span><span class="p">,</span> <span class="s1">&#39;Ximénez&#39;</span><span class="p">)</span>417<span class="go">PureWindowsPath(&#39;c:/Users/Ximénez&#39;)</span>418<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;//server/share/file&#39;</span><span class="p">)</span>419<span class="go">PureWindowsPath(&#39;//server/share/file&#39;)</span>420</pre></div>421</div>422<p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a>.</p>423</dd></dl>424 425<p>Regardless of the system you’re running on, you can instantiate all of426these classes, since they don’t provide any operation that does system calls.</p>427<section id="general-properties">428<h3>General properties<a class="headerlink" href="#general-properties" title="Link to this heading">¶</a></h3>429<p>Paths are immutable and <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>.  Paths of a same flavour are comparable430and orderable.  These properties respect the flavour’s case-folding431semantics:</p>432<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;FOO&#39;</span><span class="p">)</span>433<span class="go">False</span>434<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;FOO&#39;</span><span class="p">)</span>435<span class="go">True</span>436<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;FOO&#39;</span><span class="p">)</span> <span class="ow">in</span> <span class="p">{</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span> <span class="p">}</span>437<span class="go">True</span>438<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;C:&#39;</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;d:&#39;</span><span class="p">)</span>439<span class="go">True</span>440</pre></div>441</div>442<p>Paths of a different flavour compare unequal and cannot be ordered:</p>443<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span>444<span class="go">False</span>445<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span>446<span class="gt">Traceback (most recent call last):</span>447  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>448<span class="gr">TypeError</span>: <span class="n">&#39;&lt;&#39; not supported between instances of &#39;PureWindowsPath&#39; and &#39;PurePosixPath&#39;</span>449</pre></div>450</div>451</section>452<section id="operators">453<h3>Operators<a class="headerlink" href="#operators" title="Link to this heading">¶</a></h3>454<p>The slash operator helps create child paths, like <a class="reference internal" href="os.path.html#os.path.join" title="os.path.join"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.join()</span></code></a>.455If the argument is an absolute path, the previous path is ignored.456On Windows, the drive is not reset when the argument is a rooted457relative path (e.g., <code class="docutils literal notranslate"><span class="pre">r'\foo'</span></code>):</p>458<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span>459<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span>460<span class="go">PurePosixPath(&#39;/etc&#39;)</span>461<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">/</span> <span class="s1">&#39;init.d&#39;</span> <span class="o">/</span> <span class="s1">&#39;apache2&#39;</span>462<span class="go">PurePosixPath(&#39;/etc/init.d/apache2&#39;)</span>463<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;bin&#39;</span><span class="p">)</span>464<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;/usr&#39;</span> <span class="o">/</span> <span class="n">q</span>465<span class="go">PurePosixPath(&#39;/usr/bin&#39;)</span>466<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">/</span> <span class="s1">&#39;/an_absolute_path&#39;</span>467<span class="go">PurePosixPath(&#39;/an_absolute_path&#39;)</span>468<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Windows&#39;</span><span class="p">,</span> <span class="s1">&#39;/Program Files&#39;</span><span class="p">)</span>469<span class="go">PureWindowsPath(&#39;c:/Program Files&#39;)</span>470</pre></div>471</div>472<p>A path object can be used anywhere an object implementing <a class="reference internal" href="os.html#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.PathLike</span></code></a>473is accepted:</p>474<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">os</span>475<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span>476<span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">fspath</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>477<span class="go">&#39;/etc&#39;</span>478</pre></div>479</div>480<p>The string representation of a path is the raw filesystem path itself481(in native form, e.g. with backslashes under Windows), which you can482pass to any function taking a file path as a string:</p>483<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span>484<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>485<span class="go">&#39;/etc&#39;</span>486<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Program Files&#39;</span><span class="p">)</span>487<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>488<span class="go">&#39;c:\\Program Files&#39;</span>489</pre></div>490</div>491<p>Similarly, calling <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> on a path gives the raw filesystem path as a492bytes object, as encoded by <a class="reference internal" href="os.html#os.fsencode" title="os.fsencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fsencode()</span></code></a>:</p>493<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">bytes</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>494<span class="go">b&#39;/etc&#39;</span>495</pre></div>496</div>497<div class="admonition note">498<p class="admonition-title">Note</p>499<p>Calling <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> is only recommended under Unix.  Under Windows,500the unicode form is the canonical representation of filesystem paths.</p>501</div>502</section>503<section id="accessing-individual-parts">504<h3>Accessing individual parts<a class="headerlink" href="#accessing-individual-parts" title="Link to this heading">¶</a></h3>505<p>To access the individual “parts” (components) of a path, use the following506property:</p>507<dl class="py attribute">508<dt class="sig sig-object py" id="pathlib.PurePath.parts">509<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">parts</span></span><a class="headerlink" href="#pathlib.PurePath.parts" title="Link to this definition">¶</a></dt>510<dd><p>A tuple giving access to the path’s various components:</p>511<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/usr/bin/python3&#39;</span><span class="p">)</span>512<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parts</span>513<span class="go">(&#39;/&#39;, &#39;usr&#39;, &#39;bin&#39;, &#39;python3&#39;)</span>514 515<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Program Files/PSF&#39;</span><span class="p">)</span>516<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parts</span>517<span class="go">(&#39;c:\\&#39;, &#39;Program Files&#39;, &#39;PSF&#39;)</span>518</pre></div>519</div>520<p>(note how the drive and local root are regrouped in a single part)</p>521</dd></dl>522 523</section>524<section id="methods-and-properties">525<h3>Methods and properties<a class="headerlink" href="#methods-and-properties" title="Link to this heading">¶</a></h3>526<p>Pure paths provide the following methods and properties:</p>527<dl class="py attribute">528<dt class="sig sig-object py" id="pathlib.PurePath.parser">529<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">parser</span></span><a class="headerlink" href="#pathlib.PurePath.parser" title="Link to this definition">¶</a></dt>530<dd><p>The implementation of the <a class="reference internal" href="os.path.html#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> module used for low-level path531parsing and joining: either <code class="xref py py-mod docutils literal notranslate"><span class="pre">posixpath</span></code> or <code class="xref py py-mod docutils literal notranslate"><span class="pre">ntpath</span></code>.</p>532<div class="versionadded">533<p><span class="versionmodified added">Added in version 3.13.</span></p>534</div>535</dd></dl>536 537<dl class="py attribute">538<dt class="sig sig-object py" id="pathlib.PurePath.drive">539<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">drive</span></span><a class="headerlink" href="#pathlib.PurePath.drive" title="Link to this definition">¶</a></dt>540<dd><p>A string representing the drive letter or name, if any:</p>541<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Program Files/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">drive</span>542<span class="go">&#39;c:&#39;</span>543<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;/Program Files/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">drive</span>544<span class="go">&#39;&#39;</span>545<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">drive</span>546<span class="go">&#39;&#39;</span>547</pre></div>548</div>549<p>UNC shares are also considered drives:</p>550<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;//host/share/foo.txt&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">drive</span>551<span class="go">&#39;\\\\host\\share&#39;</span>552</pre></div>553</div>554</dd></dl>555 556<dl class="py attribute">557<dt class="sig sig-object py" id="pathlib.PurePath.root">558<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">root</span></span><a class="headerlink" href="#pathlib.PurePath.root" title="Link to this definition">¶</a></dt>559<dd><p>A string representing the (local or global) root, if any:</p>560<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Program Files/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">root</span>561<span class="go">&#39;\\&#39;</span>562<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:Program Files/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">root</span>563<span class="go">&#39;&#39;</span>564<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">root</span>565<span class="go">&#39;/&#39;</span>566</pre></div>567</div>568<p>UNC shares always have a root:</p>569<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;//host/share&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">root</span>570<span class="go">&#39;\\&#39;</span>571</pre></div>572</div>573<p>If the path starts with more than two successive slashes,574<a class="reference internal" href="#pathlib.PurePosixPath" title="pathlib.PurePosixPath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePosixPath</span></code></a> collapses them:</p>575<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;//etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">root</span>576<span class="go">&#39;//&#39;</span>577<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;///etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">root</span>578<span class="go">&#39;/&#39;</span>579<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;////etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">root</span>580<span class="go">&#39;/&#39;</span>581</pre></div>582</div>583<div class="admonition note">584<p class="admonition-title">Note</p>585<p>This behavior conforms to <em>The Open Group Base Specifications Issue 6</em>,586paragraph <a class="reference external" href="https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11">4.11 Pathname Resolution</a>:</p>587<p><em>“A pathname that begins with two successive slashes may be interpreted in588an implementation-defined manner, although more than two leading slashes589shall be treated as a single slash.”</em></p>590</div>591</dd></dl>592 593<dl class="py attribute">594<dt class="sig sig-object py" id="pathlib.PurePath.anchor">595<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">anchor</span></span><a class="headerlink" href="#pathlib.PurePath.anchor" title="Link to this definition">¶</a></dt>596<dd><p>The concatenation of the drive and root:</p>597<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Program Files/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">anchor</span>598<span class="go">&#39;c:\\&#39;</span>599<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:Program Files/&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">anchor</span>600<span class="go">&#39;c:&#39;</span>601<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">anchor</span>602<span class="go">&#39;/&#39;</span>603<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;//host/share&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">anchor</span>604<span class="go">&#39;\\\\host\\share\\&#39;</span>605</pre></div>606</div>607</dd></dl>608 609<dl class="py attribute">610<dt class="sig sig-object py" id="pathlib.PurePath.parents">611<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">parents</span></span><a class="headerlink" href="#pathlib.PurePath.parents" title="Link to this definition">¶</a></dt>612<dd><p>An immutable sequence providing access to the logical ancestors of613the path:</p>614<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/foo/bar/setup.py&#39;</span><span class="p">)</span>615<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parents</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>616<span class="go">PureWindowsPath(&#39;c:/foo/bar&#39;)</span>617<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parents</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>618<span class="go">PureWindowsPath(&#39;c:/foo&#39;)</span>619<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parents</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>620<span class="go">PureWindowsPath(&#39;c:/&#39;)</span>621</pre></div>622</div>623<div class="versionchanged">624<p><span class="versionmodified changed">Changed in version 3.10: </span>The parents sequence now supports <a class="reference internal" href="../glossary.html#term-slice"><span class="xref std std-term">slices</span></a> and negative index values.</p>625</div>626</dd></dl>627 628<dl class="py attribute">629<dt class="sig sig-object py" id="pathlib.PurePath.parent">630<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">parent</span></span><a class="headerlink" href="#pathlib.PurePath.parent" title="Link to this definition">¶</a></dt>631<dd><p>The logical parent of the path:</p>632<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/a/b/c/d&#39;</span><span class="p">)</span>633<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parent</span>634<span class="go">PurePosixPath(&#39;/a/b/c&#39;)</span>635</pre></div>636</div>637<p>You cannot go past an anchor, or empty path:</p>638<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>639<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parent</span>640<span class="go">PurePosixPath(&#39;/&#39;)</span>641<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>642<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parent</span>643<span class="go">PurePosixPath(&#39;.&#39;)</span>644</pre></div>645</div>646<div class="admonition note">647<p class="admonition-title">Note</p>648<p>This is a purely lexical operation, hence the following behaviour:</p>649<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;foo/..&#39;</span><span class="p">)</span>650<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parent</span>651<span class="go">PurePosixPath(&#39;foo&#39;)</span>652</pre></div>653</div>654<p>If you want to walk an arbitrary filesystem path upwards, it is655recommended to first call <a class="reference internal" href="#pathlib.Path.resolve" title="pathlib.Path.resolve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.resolve()</span></code></a> so as to resolve656symlinks and eliminate <code class="docutils literal notranslate"><span class="pre">&quot;..&quot;</span></code> components.</p>657</div>658</dd></dl>659 660<dl class="py attribute">661<dt class="sig sig-object py" id="pathlib.PurePath.name">662<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#pathlib.PurePath.name" title="Link to this definition">¶</a></dt>663<dd><p>A string representing the final path component, excluding the drive and664root, if any:</p>665<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library/setup.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">name</span>666<span class="go">&#39;setup.py&#39;</span>667</pre></div>668</div>669<p>UNC drive names are not considered:</p>670<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;//some/share/setup.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">name</span>671<span class="go">&#39;setup.py&#39;</span>672<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;//some/share&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">name</span>673<span class="go">&#39;&#39;</span>674</pre></div>675</div>676</dd></dl>677 678<dl class="py attribute">679<dt class="sig sig-object py" id="pathlib.PurePath.suffix">680<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">suffix</span></span><a class="headerlink" href="#pathlib.PurePath.suffix" title="Link to this definition">¶</a></dt>681<dd><p>The last dot-separated portion of the final component, if any:</p>682<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library/setup.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">suffix</span>683<span class="go">&#39;.py&#39;</span>684<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library.tar.gz&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">suffix</span>685<span class="go">&#39;.gz&#39;</span>686<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">suffix</span>687<span class="go">&#39;&#39;</span>688</pre></div>689</div>690<p>This is commonly called the file extension.</p>691<div class="versionchanged">692<p><span class="versionmodified changed">Changed in version 3.14: </span>A single dot (”<code class="docutils literal notranslate"><span class="pre">.</span></code>”) is considered a valid suffix.</p>693</div>694</dd></dl>695 696<dl class="py attribute">697<dt class="sig sig-object py" id="pathlib.PurePath.suffixes">698<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">suffixes</span></span><a class="headerlink" href="#pathlib.PurePath.suffixes" title="Link to this definition">¶</a></dt>699<dd><p>A list of the path’s suffixes, often called file extensions:</p>700<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library.tar.gar&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">suffixes</span>701<span class="go">[&#39;.tar&#39;, &#39;.gar&#39;]</span>702<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library.tar.gz&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">suffixes</span>703<span class="go">[&#39;.tar&#39;, &#39;.gz&#39;]</span>704<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">suffixes</span>705<span class="go">[]</span>706</pre></div>707</div>708<div class="versionchanged">709<p><span class="versionmodified changed">Changed in version 3.14: </span>A single dot (”<code class="docutils literal notranslate"><span class="pre">.</span></code>”) is considered a valid suffix.</p>710</div>711</dd></dl>712 713<dl class="py attribute">714<dt class="sig sig-object py" id="pathlib.PurePath.stem">715<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">stem</span></span><a class="headerlink" href="#pathlib.PurePath.stem" title="Link to this definition">¶</a></dt>716<dd><p>The final path component, without its suffix:</p>717<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library.tar.gz&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">stem</span>718<span class="go">&#39;library.tar&#39;</span>719<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library.tar&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">stem</span>720<span class="go">&#39;library&#39;</span>721<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;my/library&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">stem</span>722<span class="go">&#39;library&#39;</span>723</pre></div>724</div>725<div class="versionchanged">726<p><span class="versionmodified changed">Changed in version 3.14: </span>A single dot (”<code class="docutils literal notranslate"><span class="pre">.</span></code>”) is considered a valid suffix.</p>727</div>728</dd></dl>729 730<dl class="py method">731<dt class="sig sig-object py" id="pathlib.PurePath.as_posix">732<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">as_posix</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.as_posix" title="Link to this definition">¶</a></dt>733<dd><p>Return a string representation of the path with forward slashes (<code class="docutils literal notranslate"><span class="pre">/</span></code>):</p>734<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:</span><span class="se">\\</span><span class="s1">windows&#39;</span><span class="p">)</span>735<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>736<span class="go">&#39;c:\\windows&#39;</span>737<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">as_posix</span><span class="p">()</span>738<span class="go">&#39;c:/windows&#39;</span>739</pre></div>740</div>741</dd></dl>742 743<dl class="py method">744<dt class="sig sig-object py" id="pathlib.PurePath.is_absolute">745<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">is_absolute</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.is_absolute" title="Link to this definition">¶</a></dt>746<dd><p>Return whether the path is absolute or not.  A path is considered absolute747if it has both a root and (if the flavour allows) a drive:</p>748<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/a/b&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">is_absolute</span><span class="p">()</span>749<span class="go">True</span>750<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;a/b&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">is_absolute</span><span class="p">()</span>751<span class="go">False</span>752 753<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/a/b&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">is_absolute</span><span class="p">()</span>754<span class="go">True</span>755<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;/a/b&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">is_absolute</span><span class="p">()</span>756<span class="go">False</span>757<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">is_absolute</span><span class="p">()</span>758<span class="go">False</span>759<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;//some/share&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">is_absolute</span><span class="p">()</span>760<span class="go">True</span>761</pre></div>762</div>763</dd></dl>764 765<dl class="py method">766<dt class="sig sig-object py" id="pathlib.PurePath.is_relative_to">767<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">is_relative_to</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.is_relative_to" title="Link to this definition">¶</a></dt>768<dd><p>Return whether or not this path is relative to the <em>other</em> path.</p>769<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/etc/passwd&#39;</span><span class="p">)</span>770<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">is_relative_to</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span>771<span class="go">True</span>772<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">is_relative_to</span><span class="p">(</span><span class="s1">&#39;/usr&#39;</span><span class="p">)</span>773<span class="go">False</span>774</pre></div>775</div>776<p>This method is string-based; it neither accesses the filesystem nor treats777“<code class="docutils literal notranslate"><span class="pre">..</span></code>” segments specially. The following code is equivalent:</p>778<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">u</span> <span class="o">=</span> <span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/usr&#39;</span><span class="p">)</span>779<span class="gp">&gt;&gt;&gt; </span><span class="n">u</span> <span class="o">==</span> <span class="n">p</span> <span class="ow">or</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">p</span><span class="o">.</span><span class="n">parents</span>780<span class="go">False</span>781</pre></div>782</div>783<div class="versionadded">784<p><span class="versionmodified added">Added in version 3.9.</span></p>785</div>786<div class="deprecated-removed">787<p><span class="versionmodified removed">Deprecated since version 3.12, removed in version 3.14: </span>Passing additional arguments is deprecated; if supplied, they are joined788with <em>other</em>.</p>789</div>790</dd></dl>791 792<dl class="py method">793<dt class="sig sig-object py" id="pathlib.PurePath.joinpath">794<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">joinpath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">pathsegments</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.joinpath" title="Link to this definition">¶</a></dt>795<dd><p>Calling this method is equivalent to combining the path with each of796the given <em>pathsegments</em> in turn:</p>797<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="s1">&#39;passwd&#39;</span><span class="p">)</span>798<span class="go">PurePosixPath(&#39;/etc/passwd&#39;)</span>799<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;passwd&#39;</span><span class="p">))</span>800<span class="go">PurePosixPath(&#39;/etc/passwd&#39;)</span>801<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="s1">&#39;init.d&#39;</span><span class="p">,</span> <span class="s1">&#39;apache2&#39;</span><span class="p">)</span>802<span class="go">PurePosixPath(&#39;/etc/init.d/apache2&#39;)</span>803<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="s1">&#39;/Program Files&#39;</span><span class="p">)</span>804<span class="go">PureWindowsPath(&#39;c:/Program Files&#39;)</span>805</pre></div>806</div>807</dd></dl>808 809<dl class="py method">810<dt class="sig sig-object py" id="pathlib.PurePath.full_match">811<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">full_match</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pattern</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">case_sensitive</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.full_match" title="Link to this definition">¶</a></dt>812<dd><p>Match this path against the provided glob-style pattern.  Return <code class="docutils literal notranslate"><span class="pre">True</span></code>813if matching is successful, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.  For example:</p>814<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;a/b.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">full_match</span><span class="p">(</span><span class="s1">&#39;a/*.py&#39;</span><span class="p">)</span>815<span class="go">True</span>816<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;a/b.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">full_match</span><span class="p">(</span><span class="s1">&#39;*.py&#39;</span><span class="p">)</span>817<span class="go">False</span>818<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/a/b/c.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">full_match</span><span class="p">(</span><span class="s1">&#39;/a/**&#39;</span><span class="p">)</span>819<span class="go">True</span>820<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/a/b/c.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">full_match</span><span class="p">(</span><span class="s1">&#39;**/*.py&#39;</span><span class="p">)</span>821<span class="go">True</span>822</pre></div>823</div>824<div class="admonition seealso">825<p class="admonition-title">See also</p>826<p><a class="reference internal" href="#pathlib-pattern-language"><span class="std std-ref">Pattern language</span></a> documentation.</p>827</div>828<p>As with other methods, case-sensitivity follows platform defaults:</p>829<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;b.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">full_match</span><span class="p">(</span><span class="s1">&#39;*.PY&#39;</span><span class="p">)</span>830<span class="go">False</span>831<span class="gp">&gt;&gt;&gt; </span><span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;b.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">full_match</span><span class="p">(</span><span class="s1">&#39;*.PY&#39;</span><span class="p">)</span>832<span class="go">True</span>833</pre></div>834</div>835<p>Set <em>case_sensitive</em> to <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code> to override this behaviour.</p>836<div class="versionadded">837<p><span class="versionmodified added">Added in version 3.13.</span></p>838</div>839</dd></dl>840 841<dl class="py method">842<dt class="sig sig-object py" id="pathlib.PurePath.match">843<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">match</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pattern</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">case_sensitive</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.match" title="Link to this definition">¶</a></dt>844<dd><p>Match this path against the provided non-recursive glob-style pattern.845Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if matching is successful, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>846<p>This method is similar to <a class="reference internal" href="#pathlib.PurePath.full_match" title="pathlib.PurePath.full_match"><code class="xref py py-meth docutils literal notranslate"><span class="pre">full_match()</span></code></a>, but empty patterns847aren’t allowed (<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised), the recursive wildcard848“<code class="docutils literal notranslate"><span class="pre">**</span></code>” isn’t supported (it acts like non-recursive “<code class="docutils literal notranslate"><span class="pre">*</span></code>”), and if a849relative pattern is provided, then matching is done from the right:</p>850<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;a/b.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;*.py&#39;</span><span class="p">)</span>851<span class="go">True</span>852<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/a/b/c.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;b/*.py&#39;</span><span class="p">)</span>853<span class="go">True</span>854<span class="gp">&gt;&gt;&gt; </span><span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;/a/b/c.py&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;a/*.py&#39;</span><span class="p">)</span>855<span class="go">False</span>856</pre></div>857</div>858<div class="versionchanged">859<p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>pattern</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>860</div>861<div class="versionchanged">862<p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>case_sensitive</em> parameter was added.</p>863</div>864</dd></dl>865 866<dl class="py method">867<dt class="sig sig-object py" id="pathlib.PurePath.relative_to">868<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">relative_to</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">walk_up</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.relative_to" title="Link to this definition">¶</a></dt>869<dd><p>Compute a version of this path relative to the path represented by870<em>other</em>.  If it’s impossible, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised:</p>871<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/etc/passwd&#39;</span><span class="p">)</span>872<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">relative_to</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>873<span class="go">PurePosixPath(&#39;etc/passwd&#39;)</span>874<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">relative_to</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span>875<span class="go">PurePosixPath(&#39;passwd&#39;)</span>876<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">relative_to</span><span class="p">(</span><span class="s1">&#39;/usr&#39;</span><span class="p">)</span>877<span class="gt">Traceback (most recent call last):</span>878  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>879  File <span class="nb">&quot;pathlib.py&quot;</span>, line <span class="m">941</span>, in <span class="n">relative_to</span>880<span class="w">    </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">error_message</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">formatted</span><span class="p">)))</span>881<span class="gr">ValueError</span>: <span class="n">&#39;/etc/passwd&#39; is not in the subpath of &#39;/usr&#39; OR one path is relative and the other is absolute.</span>882</pre></div>883</div>884<p>When <em>walk_up</em> is false (the default), the path must start with <em>other</em>.885When the argument is true, <code class="docutils literal notranslate"><span class="pre">..</span></code> entries may be added to form the886relative path. In all other cases, such as the paths referencing887different drives, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.:</p>888<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">relative_to</span><span class="p">(</span><span class="s1">&#39;/usr&#39;</span><span class="p">,</span> <span class="n">walk_up</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>889<span class="go">PurePosixPath(&#39;../etc/passwd&#39;)</span>890<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">relative_to</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="n">walk_up</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>891<span class="gt">Traceback (most recent call last):</span>892  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>893  File <span class="nb">&quot;pathlib.py&quot;</span>, line <span class="m">941</span>, in <span class="n">relative_to</span>894<span class="w">    </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">error_message</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">formatted</span><span class="p">)))</span>895<span class="gr">ValueError</span>: <span class="n">&#39;/etc/passwd&#39; is not on the same drive as &#39;foo&#39; OR one path is relative and the other is absolute.</span>896</pre></div>897</div>898<div class="admonition warning">899<p class="admonition-title">Warning</p>900<p>This function is part of <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a> and works with strings.901It does not check or access the underlying file structure.902This can impact the <em>walk_up</em> option as it assumes that no symlinks903are present in the path; call <a class="reference internal" href="#pathlib.Path.resolve" title="pathlib.Path.resolve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">resolve()</span></code></a> first if904necessary to resolve symlinks.</p>905</div>906<div class="versionchanged">907<p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>walk_up</em> parameter was added (old behavior is the same as <code class="docutils literal notranslate"><span class="pre">walk_up=False</span></code>).</p>908</div>909<div class="deprecated-removed">910<p><span class="versionmodified removed">Deprecated since version 3.12, removed in version 3.14: </span>Passing additional positional arguments is deprecated; if supplied,911they are joined with <em>other</em>.</p>912</div>913</dd></dl>914 915<dl class="py method">916<dt class="sig sig-object py" id="pathlib.PurePath.with_name">917<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">with_name</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.with_name" title="Link to this definition">¶</a></dt>918<dd><p>Return a new path with the <a class="reference internal" href="#pathlib.PurePath.name" title="pathlib.PurePath.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> changed.  If the original path919doesn’t have a name, ValueError is raised:</p>920<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Downloads/pathlib.tar.gz&#39;</span><span class="p">)</span>921<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">with_name</span><span class="p">(</span><span class="s1">&#39;setup.py&#39;</span><span class="p">)</span>922<span class="go">PureWindowsPath(&#39;c:/Downloads/setup.py&#39;)</span>923<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/&#39;</span><span class="p">)</span>924<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">with_name</span><span class="p">(</span><span class="s1">&#39;setup.py&#39;</span><span class="p">)</span>925<span class="gt">Traceback (most recent call last):</span>926  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>927  File <span class="nb">&quot;/home/antoine/cpython/default/Lib/pathlib.py&quot;</span>, line <span class="m">751</span>, in <span class="n">with_name</span>928<span class="w">    </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%r</span><span class="s2"> has an empty name&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,))</span>929<span class="gr">ValueError</span>: <span class="n">PureWindowsPath(&#39;c:/&#39;) has an empty name</span>930</pre></div>931</div>932</dd></dl>933 934<dl class="py method">935<dt class="sig sig-object py" id="pathlib.PurePath.with_stem">936<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">with_stem</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stem</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.with_stem" title="Link to this definition">¶</a></dt>937<dd><p>Return a new path with the <a class="reference internal" href="#pathlib.PurePath.stem" title="pathlib.PurePath.stem"><code class="xref py py-attr docutils literal notranslate"><span class="pre">stem</span></code></a> changed.  If the original path938doesn’t have a name, ValueError is raised:</p>939<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Downloads/draft.txt&#39;</span><span class="p">)</span>940<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">with_stem</span><span class="p">(</span><span class="s1">&#39;final&#39;</span><span class="p">)</span>941<span class="go">PureWindowsPath(&#39;c:/Downloads/final.txt&#39;)</span>942<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Downloads/pathlib.tar.gz&#39;</span><span class="p">)</span>943<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">with_stem</span><span class="p">(</span><span class="s1">&#39;lib&#39;</span><span class="p">)</span>944<span class="go">PureWindowsPath(&#39;c:/Downloads/lib.gz&#39;)</span>945<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/&#39;</span><span class="p">)</span>946<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">with_stem</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">)</span>947<span class="gt">Traceback (most recent call last):</span>948  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>949  File <span class="nb">&quot;/home/antoine/cpython/default/Lib/pathlib.py&quot;</span>, line <span class="m">861</span>, in <span class="n">with_stem</span>950<span class="w">    </span><span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">with_name</span><span class="p">(</span><span class="n">stem</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">suffix</span><span class="p">)</span>951  File <span class="nb">&quot;/home/antoine/cpython/default/Lib/pathlib.py&quot;</span>, line <span class="m">851</span>, in <span class="n">with_name</span>952<span class="w">    </span><span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%r</span><span class="s2"> has an empty name&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,))</span>953<span class="gr">ValueError</span>: <span class="n">PureWindowsPath(&#39;c:/&#39;) has an empty name</span>954</pre></div>955</div>956<div class="versionadded">957<p><span class="versionmodified added">Added in version 3.9.</span></p>958</div>959</dd></dl>960 961<dl class="py method">962<dt class="sig sig-object py" id="pathlib.PurePath.with_suffix">963<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">with_suffix</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">suffix</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.with_suffix" title="Link to this definition">¶</a></dt>964<dd><p>Return a new path with the <a class="reference internal" href="#pathlib.PurePath.suffix" title="pathlib.PurePath.suffix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">suffix</span></code></a> changed.  If the original path965doesn’t have a suffix, the new <em>suffix</em> is appended instead.  If the966<em>suffix</em> is an empty string, the original suffix is removed:</p>967<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Downloads/pathlib.tar.gz&#39;</span><span class="p">)</span>968<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">with_suffix</span><span class="p">(</span><span class="s1">&#39;.bz2&#39;</span><span class="p">)</span>969<span class="go">PureWindowsPath(&#39;c:/Downloads/pathlib.tar.bz2&#39;)</span>970<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;README&#39;</span><span class="p">)</span>971<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">with_suffix</span><span class="p">(</span><span class="s1">&#39;.txt&#39;</span><span class="p">)</span>972<span class="go">PureWindowsPath(&#39;README.txt&#39;)</span>973<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PureWindowsPath</span><span class="p">(</span><span class="s1">&#39;README.txt&#39;</span><span class="p">)</span>974<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">with_suffix</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">)</span>975<span class="go">PureWindowsPath(&#39;README&#39;)</span>976</pre></div>977</div>978<div class="versionchanged">979<p><span class="versionmodified changed">Changed in version 3.14: </span>A single dot (”<code class="docutils literal notranslate"><span class="pre">.</span></code>”) is considered a valid suffix. In previous980versions, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if a single dot is supplied.</p>981</div>982</dd></dl>983 984<dl class="py method">985<dt class="sig sig-object py" id="pathlib.PurePath.with_segments">986<span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">with_segments</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">pathsegments</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.with_segments" title="Link to this definition">¶</a></dt>987<dd><p>Create a new path object of the same type by combining the given988<em>pathsegments</em>. This method is called whenever a derivative path is created,989such as from <a class="reference internal" href="#pathlib.PurePath.parent" title="pathlib.PurePath.parent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">parent</span></code></a> and <a class="reference internal" href="#pathlib.PurePath.relative_to" title="pathlib.PurePath.relative_to"><code class="xref py py-meth docutils literal notranslate"><span class="pre">relative_to()</span></code></a>. Subclasses may990override this method to pass information to derivative paths, for example:</p>991<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">pathlib</span><span class="w"> </span><span class="kn">import</span> <span class="n">PurePosixPath</span>992 993<span class="k">class</span><span class="w"> </span><span class="nc">MyPath</span><span class="p">(</span><span class="n">PurePosixPath</span><span class="p">):</span>994    <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">pathsegments</span><span class="p">,</span> <span class="n">session_id</span><span class="p">):</span>995        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">pathsegments</span><span class="p">)</span>996        <span class="bp">self</span><span class="o">.</span><span class="n">session_id</span> <span class="o">=</span> <span class="n">session_id</span>997 998    <span class="k">def</span><span class="w"> </span><span class="nf">with_segments</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">pathsegments</span><span class="p">):</span>999        <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="p">)(</span><span class="o">*</span><span class="n">pathsegments</span><span class="p">,</span> <span class="n">session_id</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">session_id</span><span class="p">)</span>1000 1001<span class="n">etc</span> <span class="o">=</span> <span class="n">MyPath</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">,</span> <span class="n">session_id</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>1002<span class="n">hosts</span> <span class="o">=</span> <span class="n">etc</span> <span class="o">/</span> <span class="s1">&#39;hosts&#39;</span>1003<span class="nb">print</span><span class="p">(</span><span class="n">hosts</span><span class="o">.</span><span class="n">session_id</span><span class="p">)</span>  <span class="c1"># 42</span>1004</pre></div>1005</div>1006<div class="versionadded">1007<p><span class="versionmodified added">Added in version 3.12.</span></p>1008</div>1009</dd></dl>1010 1011</section>1012</section>1013<section id="concrete-paths">1014<span id="id2"></span><h2>Concrete paths<a class="headerlink" href="#concrete-paths" title="Link to this heading">¶</a></h2>1015<p>Concrete paths are subclasses of the pure path classes.  In addition to1016operations provided by the latter, they also provide methods to do system1017calls on path objects.  There are three ways to instantiate concrete paths:</p>1018<dl class="py class">1019<dt class="sig sig-object py" id="pathlib.Path">1020<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pathlib.</span></span><span class="sig-name descname"><span class="pre">Path</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">pathsegments</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path" title="Link to this definition">¶</a></dt>1021<dd><p>A subclass of <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a>, this class represents concrete paths of1022the system’s path flavour (instantiating it creates either a1023<a class="reference internal" href="#pathlib.PosixPath" title="pathlib.PosixPath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PosixPath</span></code></a> or a <a class="reference internal" href="#pathlib.WindowsPath" title="pathlib.WindowsPath"><code class="xref py py-class docutils literal notranslate"><span class="pre">WindowsPath</span></code></a>):</p>1024<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;setup.py&#39;</span><span class="p">)</span>1025<span class="go">PosixPath(&#39;setup.py&#39;)</span>1026</pre></div>1027</div>1028<p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a>.</p>1029</dd></dl>1030 1031<dl class="py class">1032<dt class="sig sig-object py" id="pathlib.PosixPath">1033<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pathlib.</span></span><span class="sig-name descname"><span class="pre">PosixPath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">pathsegments</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PosixPath" title="Link to this definition">¶</a></dt>1034<dd><p>A subclass of <a class="reference internal" href="#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code></a> and <a class="reference internal" href="#pathlib.PurePosixPath" title="pathlib.PurePosixPath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePosixPath</span></code></a>, this class1035represents concrete non-Windows filesystem paths:</p>1036<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PosixPath</span><span class="p">(</span><span class="s1">&#39;/etc/hosts&#39;</span><span class="p">)</span>1037<span class="go">PosixPath(&#39;/etc/hosts&#39;)</span>1038</pre></div>1039</div>1040<p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a>.</p>1041<div class="versionchanged">1042<p><span class="versionmodified changed">Changed in version 3.13: </span>Raises <a class="reference internal" href="#pathlib.UnsupportedOperation" title="pathlib.UnsupportedOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnsupportedOperation</span></code></a> on Windows. In previous versions,1043<a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> was raised instead.</p>1044</div>1045</dd></dl>1046 1047<dl class="py class">1048<dt class="sig sig-object py" id="pathlib.WindowsPath">1049<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pathlib.</span></span><span class="sig-name descname"><span class="pre">WindowsPath</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">pathsegments</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.WindowsPath" title="Link to this definition">¶</a></dt>1050<dd><p>A subclass of <a class="reference internal" href="#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code></a> and <a class="reference internal" href="#pathlib.PureWindowsPath" title="pathlib.PureWindowsPath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PureWindowsPath</span></code></a>, this class1051represents concrete Windows filesystem paths:</p>1052<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">WindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/&#39;</span><span class="p">,</span> <span class="s1">&#39;Users&#39;</span><span class="p">,</span> <span class="s1">&#39;Ximénez&#39;</span><span class="p">)</span>1053<span class="go">WindowsPath(&#39;c:/Users/Ximénez&#39;)</span>1054</pre></div>1055</div>1056<p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a>.</p>1057<div class="versionchanged">1058<p><span class="versionmodified changed">Changed in version 3.13: </span>Raises <a class="reference internal" href="#pathlib.UnsupportedOperation" title="pathlib.UnsupportedOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnsupportedOperation</span></code></a> on non-Windows platforms. In previous1059versions, <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> was raised instead.</p>1060</div>1061</dd></dl>1062 1063<p>You can only instantiate the class flavour that corresponds to your system1064(allowing system calls on non-compatible path flavours could lead to1065bugs or failures in your application):</p>1066<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">os</span>1067<span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">name</span>1068<span class="go">&#39;posix&#39;</span>1069<span class="gp">&gt;&gt;&gt; </span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;setup.py&#39;</span><span class="p">)</span>1070<span class="go">PosixPath(&#39;setup.py&#39;)</span>1071<span class="gp">&gt;&gt;&gt; </span><span class="n">PosixPath</span><span class="p">(</span><span class="s1">&#39;setup.py&#39;</span><span class="p">)</span>1072<span class="go">PosixPath(&#39;setup.py&#39;)</span>1073<span class="gp">&gt;&gt;&gt; </span><span class="n">WindowsPath</span><span class="p">(</span><span class="s1">&#39;setup.py&#39;</span><span class="p">)</span>1074<span class="gt">Traceback (most recent call last):</span>1075  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>1076  File <span class="nb">&quot;pathlib.py&quot;</span>, line <span class="m">798</span>, in <span class="n">__new__</span>1077<span class="w">    </span><span class="o">%</span> <span class="p">(</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,))</span>1078<span class="gr">UnsupportedOperation</span>: <span class="n">cannot instantiate &#39;WindowsPath&#39; on your system</span>1079</pre></div>1080</div>1081<p>Some concrete path methods can raise an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if a system call fails1082(for example because the path doesn’t exist).</p>1083<section id="parsing-and-generating-uris">1084<h3>Parsing and generating URIs<a class="headerlink" href="#parsing-and-generating-uris" title="Link to this heading">¶</a></h3>1085<p>Concrete path objects can be created from, and represented as, ‘file’ URIs1086conforming to <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc8089.html"><strong>RFC 8089</strong></a>.</p>1087<div class="admonition note">1088<p class="admonition-title">Note</p>1089<p>File URIs are not portable across machines with different1090<a class="reference internal" href="os.html#filesystem-encoding"><span class="std std-ref">filesystem encodings</span></a>.</p>1091</div>1092<dl class="py method">1093<dt class="sig sig-object py" id="pathlib.Path.from_uri">1094<em class="property"><span class="k"><span class="pre">classmethod</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">from_uri</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">uri</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.from_uri" title="Link to this definition">¶</a></dt>1095<dd><p>Return a new path object from parsing a ‘file’ URI. For example:</p>1096<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Path</span><span class="o">.</span><span class="n">from_uri</span><span class="p">(</span><span class="s1">&#39;file:///etc/hosts&#39;</span><span class="p">)</span>1097<span class="go">PosixPath(&#39;/etc/hosts&#39;)</span>1098</pre></div>1099</div>1100<p>On Windows, DOS device and UNC paths may be parsed from URIs:</p>1101<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Path</span><span class="o">.</span><span class="n">from_uri</span><span class="p">(</span><span class="s1">&#39;file:///c:/windows&#39;</span><span class="p">)</span>1102<span class="go">WindowsPath(&#39;c:/windows&#39;)</span>1103<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Path</span><span class="o">.</span><span class="n">from_uri</span><span class="p">(</span><span class="s1">&#39;file://server/share&#39;</span><span class="p">)</span>1104<span class="go">WindowsPath(&#39;//server/share&#39;)</span>1105</pre></div>1106</div>1107<p>Several variant forms are supported:</p>1108<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Path</span><span class="o">.</span><span class="n">from_uri</span><span class="p">(</span><span class="s1">&#39;file:////server/share&#39;</span><span class="p">)</span>1109<span class="go">WindowsPath(&#39;//server/share&#39;)</span>1110<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Path</span><span class="o">.</span><span class="n">from_uri</span><span class="p">(</span><span class="s1">&#39;file://///server/share&#39;</span><span class="p">)</span>1111<span class="go">WindowsPath(&#39;//server/share&#39;)</span>1112<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Path</span><span class="o">.</span><span class="n">from_uri</span><span class="p">(</span><span class="s1">&#39;file:c:/windows&#39;</span><span class="p">)</span>1113<span class="go">WindowsPath(&#39;c:/windows&#39;)</span>1114<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Path</span><span class="o">.</span><span class="n">from_uri</span><span class="p">(</span><span class="s1">&#39;file:/c|/windows&#39;</span><span class="p">)</span>1115<span class="go">WindowsPath(&#39;c:/windows&#39;)</span>1116</pre></div>1117</div>1118<p><a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if the URI does not start with <code class="docutils literal notranslate"><span class="pre">file:</span></code>, or1119the parsed path isn’t absolute.</p>1120<div class="versionadded">1121<p><span class="versionmodified added">Added in version 3.13.</span></p>1122</div>1123<div class="versionchanged">1124<p><span class="versionmodified changed">Changed in version 3.14: </span>The URL authority is discarded if it matches the local hostname.1125Otherwise, if the authority isn’t empty or <code class="docutils literal notranslate"><span class="pre">localhost</span></code>, then on1126Windows a UNC path is returned (as before), and on other platforms a1127<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>1128</div>1129</dd></dl>1130 1131<dl class="py method">1132<dt class="sig sig-object py" id="pathlib.Path.as_uri">1133<span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">as_uri</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.as_uri" title="Link to this definition">¶</a></dt>1134<dd><p>Represent the path as a ‘file’ URI.  <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if1135the path isn’t absolute.</p>1136<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PosixPath</span><span class="p">(</span><span class="s1">&#39;/etc/passwd&#39;</span><span class="p">)</span>1137<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">as_uri</span><span class="p">()</span>1138<span class="go">&#39;file:///etc/passwd&#39;</span>1139<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">WindowsPath</span><span class="p">(</span><span class="s1">&#39;c:/Windows&#39;</span><span class="p">)</span>1140<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">as_uri</span><span class="p">()</span>1141<span class="go">&#39;file:///c:/Windows&#39;</span>1142</pre></div>1143</div>1144<div class="deprecated-removed">1145<p><span class="versionmodified deprecated">Deprecated since version 3.14, will be removed in version 3.19: </span>Calling this method from <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code class="xref py py-class docutils literal notranslate"><span class="pre">PurePath</span></code></a> rather than <a class="reference internal" href="#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code></a> is1146possible but deprecated. The method’s use of <a class="reference internal" href="os.html#os.fsencode" title="os.fsencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fsencode()</span></code></a> makes1147it strictly impure.</p>1148</div>1149</dd></dl>1150 1151</section>1152<section id="expanding-and-resolving-paths">1153<h3>Expanding and resolving paths<a class="headerlink" href="#expanding-and-resolving-paths" title="Link to this heading">¶</a></h3>1154<dl class="py method">1155<dt class="sig sig-object py" id="pathlib.Path.home">1156<em class="property"><span class="k"><span class="pre">classmethod</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">home</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.home" title="Link to this definition">¶</a></dt>1157<dd><p>Return a new path object representing the user’s home directory (as1158returned by <a class="reference internal" href="os.path.html#os.path.expanduser" title="os.path.expanduser"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.expanduser()</span></code></a> with <code class="docutils literal notranslate"><span class="pre">~</span></code> construct). If the home1159directory can’t be resolved, <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.</p>1160<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Path</span><span class="o">.</span><span class="n">home</span><span class="p">()</span>1161<span class="go">PosixPath(&#39;/home/antoine&#39;)</span>1162</pre></div>1163</div>1164<div class="versionadded">1165<p><span class="versionmodified added">Added in version 3.5.</span></p>1166</div>1167</dd></dl>1168 1169<dl class="py method">1170<dt class="sig sig-object py" id="pathlib.Path.expanduser">1171<span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">expanduser</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.expanduser" title="Link to this definition">¶</a></dt>1172<dd><p>Return a new path with expanded <code class="docutils literal notranslate"><span class="pre">~</span></code> and <code class="docutils literal notranslate"><span class="pre">~user</span></code> constructs,1173as returned by <a class="reference internal" href="os.path.html#os.path.expanduser" title="os.path.expanduser"><code class="xref py py-meth docutils literal notranslate"><span class="pre">os.path.expanduser()</span></code></a>. If a home directory can’t be1174resolved, <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.</p>1175<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">PosixPath</span><span class="p">(</span><span class="s1">&#39;~/films/Monty Python&#39;</span><span class="p">)</span>1176<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">expanduser</span><span class="p">()</span>1177<span class="go">PosixPath(&#39;/home/eric/films/Monty Python&#39;)</span>1178</pre></div>1179</div>1180<div class="versionadded">1181<p><span class="versionmodified added">Added in version 3.5.</span></p>1182</div>1183</dd></dl>1184 1185<dl class="py method">1186<dt class="sig sig-object py" id="pathlib.Path.cwd">1187<em class="property"><span class="k"><span class="pre">classmethod</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">cwd</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.cwd" title="Link to this definition">¶</a></dt>1188<dd><p>Return a new path object representing the current directory (as returned1189by <a class="reference internal" href="os.html#os.getcwd" title="os.getcwd"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.getcwd()</span></code></a>):</p>1190<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Path</span><span class="o">.</span><span class="n">cwd</span><span class="p">()</span>1191<span class="go">PosixPath(&#39;/home/antoine/pathlib&#39;)</span>1192</pre></div>1193</div>1194</dd></dl>1195 1196<dl class="py method">1197<dt class="sig sig-object py" id="pathlib.Path.absolute">1198<span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">absolute</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.absolute" title="Link to this definition">¶</a></dt>1199<dd><p>Make the path absolute, without normalization or resolving symlinks.1200Returns a new path object:</p>

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