gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

2009 lines
200KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="pathlib — Object-oriented filesystem paths" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/pathlib.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/pathlib.py This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which p..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/pathlib.py This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which p..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>pathlib — Object-oriented filesystem paths &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="os.path — Common pathname manipulations" href="os.path.html" />
  33. <link rel="prev" title="File and Directory Access" href="filesys.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/pathlib.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code> — Object-oriented filesystem paths</a><ul>
  86. <li><a class="reference internal" href="#basic-use">Basic use</a></li>
  87. <li><a class="reference internal" href="#pure-paths">Pure paths</a><ul>
  88. <li><a class="reference internal" href="#general-properties">General properties</a></li>
  89. <li><a class="reference internal" href="#operators">Operators</a></li>
  90. <li><a class="reference internal" href="#accessing-individual-parts">Accessing individual parts</a></li>
  91. <li><a class="reference internal" href="#methods-and-properties">Methods and properties</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#concrete-paths">Concrete paths</a><ul>
  95. <li><a class="reference internal" href="#methods">Methods</a></li>
  96. </ul>
  97. </li>
  98. <li><a class="reference internal" href="#correspondence-to-tools-in-the-os-module">Correspondence to tools in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code> module</a></li>
  99. </ul>
  100. </li>
  101. </ul>
  102. </div>
  103. <div>
  104. <h4>Previous topic</h4>
  105. <p class="topless"><a href="filesys.html"
  106. title="previous chapter">File and Directory Access</a></p>
  107. </div>
  108. <div>
  109. <h4>Next topic</h4>
  110. <p class="topless"><a href="os.path.html"
  111. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code> — Common pathname manipulations</a></p>
  112. </div>
  113. <div role="note" aria-label="source link">
  114. <h3>This Page</h3>
  115. <ul class="this-page-menu">
  116. <li><a href="../bugs.html">Report a Bug</a></li>
  117. <li>
  118. <a href="https://github.com/python/cpython/blob/main/Doc/library/pathlib.rst"
  119. rel="nofollow">Show Source
  120. </a>
  121. </li>
  122. </ul>
  123. </div>
  124. </nav>
  125. </div>
  126. </div>
  127. <div class="related" role="navigation" aria-label="related navigation">
  128. <h3>Navigation</h3>
  129. <ul>
  130. <li class="right" style="margin-right: 10px">
  131. <a href="../genindex.html" title="General Index"
  132. accesskey="I">index</a></li>
  133. <li class="right" >
  134. <a href="../py-modindex.html" title="Python Module Index"
  135. >modules</a> |</li>
  136. <li class="right" >
  137. <a href="os.path.html" title="os.path — Common pathname manipulations"
  138. accesskey="N">next</a> |</li>
  139. <li class="right" >
  140. <a href="filesys.html" title="File and Directory Access"
  141. accesskey="P">previous</a> |</li>
  142. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  143. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  144. <li class="switchers">
  145. <div class="language_switcher_placeholder"></div>
  146. <div class="version_switcher_placeholder"></div>
  147. </li>
  148. <li>
  149. </li>
  150. <li id="cpython-language-and-version">
  151. <a href="../index.html">3.12.3 Documentation</a> &#187;
  152. </li>
  153. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  154. <li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">File and Directory Access</a> &#187;</li>
  155. <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>
  156. <li class="right">
  157. <div class="inline-search" role="search">
  158. <form class="inline-search" action="../search.html" method="get">
  159. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  160. <input type="submit" value="Go" />
  161. </form>
  162. </div>
  163. |
  164. </li>
  165. <li class="right">
  166. <label class="theme-selector-label">
  167. Theme
  168. <select class="theme-selector" oninput="activateTheme(this.value)">
  169. <option value="auto" selected>Auto</option>
  170. <option value="light">Light</option>
  171. <option value="dark">Dark</option>
  172. </select>
  173. </label> |</li>
  174. </ul>
  175. </div>
  176. <div class="document">
  177. <div class="documentwrapper">
  178. <div class="bodywrapper">
  179. <div class="body" role="main">
  180. <section id="module-pathlib">
  181. <span id="pathlib-object-oriented-filesystem-paths"></span><h1><a class="reference internal" href="#module-pathlib" title="pathlib: Object-oriented filesystem paths"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code></a> — Object-oriented filesystem paths<a class="headerlink" href="#module-pathlib" title="Link to this heading">¶</a></h1>
  182. <div class="versionadded">
  183. <p><span class="versionmodified added">New in version 3.4.</span></p>
  184. </div>
  185. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/pathlib.py">Lib/pathlib.py</a></p>
  186. <hr class="docutils" id="index-0" />
  187. <p>This module offers classes representing filesystem paths with semantics
  188. appropriate for different operating systems. Path classes are divided
  189. between <a class="reference internal" href="#pure-paths"><span class="std std-ref">pure paths</span></a>, which provide purely computational
  190. operations without I/O, and <a class="reference internal" href="#concrete-paths"><span class="std std-ref">concrete paths</span></a>, which
  191. inherit from pure paths but also provide I/O operations.</p>
  192. <img alt="../_images/pathlib-inheritance.png" class="invert-in-dark-mode align-center" src="../_images/pathlib-inheritance.png" />
  193. <p>If you’ve never used this module before or just aren’t sure which class is
  194. right 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 instantiates
  195. a <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>
  196. <p>Pure paths are useful in some special cases; for example:</p>
  197. <ol class="arabic simple">
  198. <li><p>If you want to manipulate Windows paths on a Unix machine (or vice versa).
  199. You 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 you
  200. can 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>
  201. <li><p>You want to make sure that your code only manipulates paths without actually
  202. accessing the OS. In this case, instantiating one of the pure classes may be
  203. useful since those simply don’t have any OS-accessing operations.</p></li>
  204. </ol>
  205. <div class="admonition seealso">
  206. <p class="admonition-title">See also</p>
  207. <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>
  208. </div>
  209. <div class="admonition seealso">
  210. <p class="admonition-title">See also</p>
  211. <p>For low-level path manipulation on strings, you can also use the
  212. <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>
  213. </div>
  214. <section id="basic-use">
  215. <h2>Basic use<a class="headerlink" href="#basic-use" title="Link to this heading">¶</a></h2>
  216. <p>Importing the main class:</p>
  217. <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="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
  218. </pre></div>
  219. </div>
  220. <p>Listing subdirectories:</p>
  221. <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>
  222. <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>
  223. <span class="go">[PosixPath(&#39;.hg&#39;), PosixPath(&#39;docs&#39;), PosixPath(&#39;dist&#39;),</span>
  224. <span class="go"> PosixPath(&#39;__pycache__&#39;), PosixPath(&#39;build&#39;)]</span>
  225. </pre></div>
  226. </div>
  227. <p>Listing Python source files in this directory tree:</p>
  228. <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>
  229. <span class="go">[PosixPath(&#39;test_pathlib.py&#39;), PosixPath(&#39;setup.py&#39;),</span>
  230. <span class="go"> PosixPath(&#39;pathlib.py&#39;), PosixPath(&#39;docs/conf.py&#39;),</span>
  231. <span class="go"> PosixPath(&#39;build/lib/pathlib.py&#39;)]</span>
  232. </pre></div>
  233. </div>
  234. <p>Navigating inside a directory tree:</p>
  235. <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>
  236. <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>
  237. <span class="gp">&gt;&gt;&gt; </span><span class="n">q</span>
  238. <span class="go">PosixPath(&#39;/etc/init.d/reboot&#39;)</span>
  239. <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>
  240. <span class="go">PosixPath(&#39;/etc/rc.d/init.d/halt&#39;)</span>
  241. </pre></div>
  242. </div>
  243. <p>Querying path properties:</p>
  244. <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>
  245. <span class="go">True</span>
  246. <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>
  247. <span class="go">False</span>
  248. </pre></div>
  249. </div>
  250. <p>Opening a file:</p>
  251. <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>
  252. <span class="gp">...</span>
  253. <span class="go">&#39;#!/bin/bash\n&#39;</span>
  254. </pre></div>
  255. </div>
  256. </section>
  257. <section id="pure-paths">
  258. <span id="id1"></span><h2>Pure paths<a class="headerlink" href="#pure-paths" title="Link to this heading">¶</a></h2>
  259. <p>Pure path objects provide path-handling operations which don’t actually
  260. access a filesystem. There are three ways to access these classes, which
  261. we also call <em>flavours</em>:</p>
  262. <dl class="py class">
  263. <dt class="sig sig-object py" id="pathlib.PurePath">
  264. <em class="property"><span class="pre">class</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>
  265. <dd><p>A generic class that represents the system’s path flavour (instantiating
  266. it 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>
  267. <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>
  268. <span class="go">PurePosixPath(&#39;setup.py&#39;)</span>
  269. </pre></div>
  270. </div>
  271. <p>Each element of <em>pathsegments</em> can be either a string representing a
  272. path 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> interface
  273. where 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,
  274. such as another path object:</p>
  275. <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>
  276. <span class="go">PurePosixPath(&#39;foo/some/path/bar&#39;)</span>
  277. <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>
  278. <span class="go">PurePosixPath(&#39;foo/bar&#39;)</span>
  279. </pre></div>
  280. </div>
  281. <p>When <em>pathsegments</em> is empty, the current directory is assumed:</p>
  282. <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>
  283. <span class="go">PurePosixPath(&#39;.&#39;)</span>
  284. </pre></div>
  285. </div>
  286. <p>If a segment is an absolute path, all previous segments are ignored
  287. (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>
  288. <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>
  289. <span class="go">PurePosixPath(&#39;/usr/lib64&#39;)</span>
  290. <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>
  291. <span class="go">PureWindowsPath(&#39;d:bar&#39;)</span>
  292. </pre></div>
  293. </div>
  294. <p>On Windows, the drive is not reset when a rooted relative path
  295. segment (e.g., <code class="docutils literal notranslate"><span class="pre">r'\foo'</span></code>) is encountered:</p>
  296. <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>
  297. <span class="go">PureWindowsPath(&#39;c:/Program Files&#39;)</span>
  298. </pre></div>
  299. </div>
  300. <p>Spurious slashes and single dots are collapsed, but double dots (<code class="docutils literal notranslate"><span class="pre">'..'</span></code>)
  301. and leading double slashes (<code class="docutils literal notranslate"><span class="pre">'//'</span></code>) are not, since this would change the
  302. meaning of a path for various reasons (e.g. symbolic links, UNC paths):</p>
  303. <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>
  304. <span class="go">PurePosixPath(&#39;foo/bar&#39;)</span>
  305. <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>
  306. <span class="go">PurePosixPath(&#39;//foo/bar&#39;)</span>
  307. <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>
  308. <span class="go">PurePosixPath(&#39;foo/bar&#39;)</span>
  309. <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>
  310. <span class="go">PurePosixPath(&#39;foo/../bar&#39;)</span>
  311. </pre></div>
  312. </div>
  313. <p>(a naïve approach would make <code class="docutils literal notranslate"><span class="pre">PurePosixPath('foo/../bar')</span></code> equivalent
  314. to <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 link
  315. to another directory)</p>
  316. <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 them
  317. to be used anywhere the interface is accepted.</p>
  318. <div class="versionchanged">
  319. <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>
  320. </div>
  321. </dd></dl>
  322. <dl class="py class">
  323. <dt class="sig sig-object py" id="pathlib.PurePosixPath">
  324. <em class="property"><span class="pre">class</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>
  325. <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-Windows
  326. filesystem paths:</p>
  327. <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>
  328. <span class="go">PurePosixPath(&#39;/etc&#39;)</span>
  329. </pre></div>
  330. </div>
  331. <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>
  332. </dd></dl>
  333. <dl class="py class">
  334. <dt class="sig sig-object py" id="pathlib.PureWindowsPath">
  335. <em class="property"><span class="pre">class</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>
  336. <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 Windows
  337. filesystem paths, including <a class="reference external" href="https://en.wikipedia.org/wiki/Path_(computing)#UNC">UNC paths</a>:</p>
  338. <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>
  339. <span class="go">PureWindowsPath(&#39;c:/Program Files&#39;)</span>
  340. <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>
  341. <span class="go">PureWindowsPath(&#39;//server/share/file&#39;)</span>
  342. </pre></div>
  343. </div>
  344. <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>
  345. </dd></dl>
  346. <p>Regardless of the system you’re running on, you can instantiate all of
  347. these classes, since they don’t provide any operation that does system calls.</p>
  348. <section id="general-properties">
  349. <h3>General properties<a class="headerlink" href="#general-properties" title="Link to this heading">¶</a></h3>
  350. <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 comparable
  351. and orderable. These properties respect the flavour’s case-folding
  352. semantics:</p>
  353. <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>
  354. <span class="go">False</span>
  355. <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>
  356. <span class="go">True</span>
  357. <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>
  358. <span class="go">True</span>
  359. <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>
  360. <span class="go">True</span>
  361. </pre></div>
  362. </div>
  363. <p>Paths of a different flavour compare unequal and cannot be ordered:</p>
  364. <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>
  365. <span class="go">False</span>
  366. <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>
  367. <span class="gt">Traceback (most recent call last):</span>
  368. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  369. <span class="gr">TypeError</span>: <span class="n">&#39;&lt;&#39; not supported between instances of &#39;PureWindowsPath&#39; and &#39;PurePosixPath&#39;</span>
  370. </pre></div>
  371. </div>
  372. </section>
  373. <section id="operators">
  374. <h3>Operators<a class="headerlink" href="#operators" title="Link to this heading">¶</a></h3>
  375. <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>.
  376. If the argument is an absolute path, the previous path is ignored.
  377. On Windows, the drive is not reset when the argument is a rooted
  378. relative path (e.g., <code class="docutils literal notranslate"><span class="pre">r'\foo'</span></code>):</p>
  379. <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>
  380. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span>
  381. <span class="go">PurePosixPath(&#39;/etc&#39;)</span>
  382. <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>
  383. <span class="go">PurePosixPath(&#39;/etc/init.d/apache2&#39;)</span>
  384. <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>
  385. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;/usr&#39;</span> <span class="o">/</span> <span class="n">q</span>
  386. <span class="go">PurePosixPath(&#39;/usr/bin&#39;)</span>
  387. <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>
  388. <span class="go">PurePosixPath(&#39;/an_absolute_path&#39;)</span>
  389. <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>
  390. <span class="go">PureWindowsPath(&#39;c:/Program Files&#39;)</span>
  391. </pre></div>
  392. </div>
  393. <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>
  394. is accepted:</p>
  395. <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="nn">os</span>
  396. <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>
  397. <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>
  398. <span class="go">&#39;/etc&#39;</span>
  399. </pre></div>
  400. </div>
  401. <p>The string representation of a path is the raw filesystem path itself
  402. (in native form, e.g. with backslashes under Windows), which you can
  403. pass to any function taking a file path as a string:</p>
  404. <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>
  405. <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>
  406. <span class="go">&#39;/etc&#39;</span>
  407. <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>
  408. <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>
  409. <span class="go">&#39;c:\\Program Files&#39;</span>
  410. </pre></div>
  411. </div>
  412. <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 a
  413. bytes 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>
  414. <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>
  415. <span class="go">b&#39;/etc&#39;</span>
  416. </pre></div>
  417. </div>
  418. <div class="admonition note">
  419. <p class="admonition-title">Note</p>
  420. <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,
  421. the unicode form is the canonical representation of filesystem paths.</p>
  422. </div>
  423. </section>
  424. <section id="accessing-individual-parts">
  425. <h3>Accessing individual parts<a class="headerlink" href="#accessing-individual-parts" title="Link to this heading">¶</a></h3>
  426. <p>To access the individual “parts” (components) of a path, use the following
  427. property:</p>
  428. <dl class="py attribute">
  429. <dt class="sig sig-object py" id="pathlib.PurePath.parts">
  430. <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>
  431. <dd><p>A tuple giving access to the path’s various components:</p>
  432. <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>
  433. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parts</span>
  434. <span class="go">(&#39;/&#39;, &#39;usr&#39;, &#39;bin&#39;, &#39;python3&#39;)</span>
  435. <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>
  436. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parts</span>
  437. <span class="go">(&#39;c:\\&#39;, &#39;Program Files&#39;, &#39;PSF&#39;)</span>
  438. </pre></div>
  439. </div>
  440. <p>(note how the drive and local root are regrouped in a single part)</p>
  441. </dd></dl>
  442. </section>
  443. <section id="methods-and-properties">
  444. <h3>Methods and properties<a class="headerlink" href="#methods-and-properties" title="Link to this heading">¶</a></h3>
  445. <p>Pure paths provide the following methods and properties:</p>
  446. <dl class="py attribute">
  447. <dt class="sig sig-object py" id="pathlib.PurePath.drive">
  448. <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>
  449. <dd><p>A string representing the drive letter or name, if any:</p>
  450. <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>
  451. <span class="go">&#39;c:&#39;</span>
  452. <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>
  453. <span class="go">&#39;&#39;</span>
  454. <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>
  455. <span class="go">&#39;&#39;</span>
  456. </pre></div>
  457. </div>
  458. <p>UNC shares are also considered drives:</p>
  459. <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>
  460. <span class="go">&#39;\\\\host\\share&#39;</span>
  461. </pre></div>
  462. </div>
  463. </dd></dl>
  464. <dl class="py attribute">
  465. <dt class="sig sig-object py" id="pathlib.PurePath.root">
  466. <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>
  467. <dd><p>A string representing the (local or global) root, if any:</p>
  468. <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>
  469. <span class="go">&#39;\\&#39;</span>
  470. <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>
  471. <span class="go">&#39;&#39;</span>
  472. <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>
  473. <span class="go">&#39;/&#39;</span>
  474. </pre></div>
  475. </div>
  476. <p>UNC shares always have a root:</p>
  477. <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>
  478. <span class="go">&#39;\\&#39;</span>
  479. </pre></div>
  480. </div>
  481. <p>If the path starts with more than two successive slashes,
  482. <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>
  483. <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>
  484. <span class="go">&#39;//&#39;</span>
  485. <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>
  486. <span class="go">&#39;/&#39;</span>
  487. <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>
  488. <span class="go">&#39;/&#39;</span>
  489. </pre></div>
  490. </div>
  491. <div class="admonition note">
  492. <p class="admonition-title">Note</p>
  493. <p>This behavior conforms to <em>The Open Group Base Specifications Issue 6</em>,
  494. paragraph <a class="reference external" href="https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11">4.11 Pathname Resolution</a>:</p>
  495. <p><em>“A pathname that begins with two successive slashes may be interpreted in
  496. an implementation-defined manner, although more than two leading slashes
  497. shall be treated as a single slash.”</em></p>
  498. </div>
  499. </dd></dl>
  500. <dl class="py attribute">
  501. <dt class="sig sig-object py" id="pathlib.PurePath.anchor">
  502. <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>
  503. <dd><p>The concatenation of the drive and root:</p>
  504. <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>
  505. <span class="go">&#39;c:\\&#39;</span>
  506. <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>
  507. <span class="go">&#39;c:&#39;</span>
  508. <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>
  509. <span class="go">&#39;/&#39;</span>
  510. <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>
  511. <span class="go">&#39;\\\\host\\share\\&#39;</span>
  512. </pre></div>
  513. </div>
  514. </dd></dl>
  515. <dl class="py attribute">
  516. <dt class="sig sig-object py" id="pathlib.PurePath.parents">
  517. <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>
  518. <dd><p>An immutable sequence providing access to the logical ancestors of
  519. the path:</p>
  520. <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>
  521. <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>
  522. <span class="go">PureWindowsPath(&#39;c:/foo/bar&#39;)</span>
  523. <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>
  524. <span class="go">PureWindowsPath(&#39;c:/foo&#39;)</span>
  525. <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>
  526. <span class="go">PureWindowsPath(&#39;c:/&#39;)</span>
  527. </pre></div>
  528. </div>
  529. <div class="versionchanged">
  530. <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>
  531. </div>
  532. </dd></dl>
  533. <dl class="py attribute">
  534. <dt class="sig sig-object py" id="pathlib.PurePath.parent">
  535. <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>
  536. <dd><p>The logical parent of the path:</p>
  537. <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>
  538. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parent</span>
  539. <span class="go">PurePosixPath(&#39;/a/b/c&#39;)</span>
  540. </pre></div>
  541. </div>
  542. <p>You cannot go past an anchor, or empty path:</p>
  543. <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>
  544. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parent</span>
  545. <span class="go">PurePosixPath(&#39;/&#39;)</span>
  546. <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>
  547. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parent</span>
  548. <span class="go">PurePosixPath(&#39;.&#39;)</span>
  549. </pre></div>
  550. </div>
  551. <div class="admonition note">
  552. <p class="admonition-title">Note</p>
  553. <p>This is a purely lexical operation, hence the following behaviour:</p>
  554. <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>
  555. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">parent</span>
  556. <span class="go">PurePosixPath(&#39;foo&#39;)</span>
  557. </pre></div>
  558. </div>
  559. <p>If you want to walk an arbitrary filesystem path upwards, it is
  560. recommended 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 resolve
  561. symlinks and eliminate <code class="docutils literal notranslate"><span class="pre">&quot;..&quot;</span></code> components.</p>
  562. </div>
  563. </dd></dl>
  564. <dl class="py attribute">
  565. <dt class="sig sig-object py" id="pathlib.PurePath.name">
  566. <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>
  567. <dd><p>A string representing the final path component, excluding the drive and
  568. root, if any:</p>
  569. <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>
  570. <span class="go">&#39;setup.py&#39;</span>
  571. </pre></div>
  572. </div>
  573. <p>UNC drive names are not considered:</p>
  574. <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>
  575. <span class="go">&#39;setup.py&#39;</span>
  576. <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>
  577. <span class="go">&#39;&#39;</span>
  578. </pre></div>
  579. </div>
  580. </dd></dl>
  581. <dl class="py attribute">
  582. <dt class="sig sig-object py" id="pathlib.PurePath.suffix">
  583. <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>
  584. <dd><p>The file extension of the final component, if any:</p>
  585. <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>
  586. <span class="go">&#39;.py&#39;</span>
  587. <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>
  588. <span class="go">&#39;.gz&#39;</span>
  589. <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>
  590. <span class="go">&#39;&#39;</span>
  591. </pre></div>
  592. </div>
  593. </dd></dl>
  594. <dl class="py attribute">
  595. <dt class="sig sig-object py" id="pathlib.PurePath.suffixes">
  596. <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>
  597. <dd><p>A list of the path’s file extensions:</p>
  598. <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>
  599. <span class="go">[&#39;.tar&#39;, &#39;.gar&#39;]</span>
  600. <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>
  601. <span class="go">[&#39;.tar&#39;, &#39;.gz&#39;]</span>
  602. <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>
  603. <span class="go">[]</span>
  604. </pre></div>
  605. </div>
  606. </dd></dl>
  607. <dl class="py attribute">
  608. <dt class="sig sig-object py" id="pathlib.PurePath.stem">
  609. <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>
  610. <dd><p>The final path component, without its suffix:</p>
  611. <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>
  612. <span class="go">&#39;library.tar&#39;</span>
  613. <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>
  614. <span class="go">&#39;library&#39;</span>
  615. <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>
  616. <span class="go">&#39;library&#39;</span>
  617. </pre></div>
  618. </div>
  619. </dd></dl>
  620. <dl class="py method">
  621. <dt class="sig sig-object py" id="pathlib.PurePath.as_posix">
  622. <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>
  623. <dd><p>Return a string representation of the path with forward slashes (<code class="docutils literal notranslate"><span class="pre">/</span></code>):</p>
  624. <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>
  625. <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>
  626. <span class="go">&#39;c:\\windows&#39;</span>
  627. <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>
  628. <span class="go">&#39;c:/windows&#39;</span>
  629. </pre></div>
  630. </div>
  631. </dd></dl>
  632. <dl class="py method">
  633. <dt class="sig sig-object py" id="pathlib.PurePath.as_uri">
  634. <span class="sig-prename descclassname"><span class="pre">PurePath.</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.PurePath.as_uri" title="Link to this definition">¶</a></dt>
  635. <dd><p>Represent the path as a <code class="docutils literal notranslate"><span class="pre">file</span></code> 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 if
  636. the path isn’t absolute.</p>
  637. <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">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;/etc/passwd&#39;</span><span class="p">)</span>
  638. <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>
  639. <span class="go">&#39;file:///etc/passwd&#39;</span>
  640. <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:/Windows&#39;</span><span class="p">)</span>
  641. <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>
  642. <span class="go">&#39;file:///c:/Windows&#39;</span>
  643. </pre></div>
  644. </div>
  645. </dd></dl>
  646. <dl class="py method">
  647. <dt class="sig sig-object py" id="pathlib.PurePath.is_absolute">
  648. <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>
  649. <dd><p>Return whether the path is absolute or not. A path is considered absolute
  650. if it has both a root and (if the flavour allows) a drive:</p>
  651. <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>
  652. <span class="go">True</span>
  653. <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>
  654. <span class="go">False</span>
  655. <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>
  656. <span class="go">True</span>
  657. <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>
  658. <span class="go">False</span>
  659. <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>
  660. <span class="go">False</span>
  661. <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>
  662. <span class="go">True</span>
  663. </pre></div>
  664. </div>
  665. </dd></dl>
  666. <dl class="py method">
  667. <dt class="sig sig-object py" id="pathlib.PurePath.is_relative_to">
  668. <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>
  669. <dd><p>Return whether or not this path is relative to the <em>other</em> path.</p>
  670. <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>
  671. <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>
  672. <span class="go">True</span>
  673. <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>
  674. <span class="go">False</span>
  675. </pre></div>
  676. </div>
  677. <p>This method is string-based; it neither accesses the filesystem nor treats
  678. “<code class="docutils literal notranslate"><span class="pre">..</span></code>” segments specially. The following code is equivalent:</p>
  679. <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>
  680. <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>
  681. <span class="go">False</span>
  682. </pre></div>
  683. </div>
  684. <div class="versionadded">
  685. <p><span class="versionmodified added">New in version 3.9.</span></p>
  686. </div>
  687. <div class="deprecated-removed">
  688. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>Passing additional arguments is deprecated; if supplied, they are joined
  689. with <em>other</em>.</p>
  690. </div>
  691. </dd></dl>
  692. <dl class="py method">
  693. <dt class="sig sig-object py" id="pathlib.PurePath.is_reserved">
  694. <span class="sig-prename descclassname"><span class="pre">PurePath.</span></span><span class="sig-name descname"><span class="pre">is_reserved</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.PurePath.is_reserved" title="Link to this definition">¶</a></dt>
  695. <dd><p>With <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>, return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path is considered
  696. reserved under Windows, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. With <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>,
  697. <code class="docutils literal notranslate"><span class="pre">False</span></code> is always returned.</p>
  698. <div class="doctest highlight-default 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;nul&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">is_reserved</span><span class="p">()</span>
  699. <span class="go">True</span>
  700. <span class="gp">&gt;&gt;&gt; </span><span class="n">PurePosixPath</span><span class="p">(</span><span class="s1">&#39;nul&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">is_reserved</span><span class="p">()</span>
  701. <span class="go">False</span>
  702. </pre></div>
  703. </div>
  704. <p>File system calls on reserved paths can fail mysteriously or have
  705. unintended effects.</p>
  706. </dd></dl>
  707. <dl class="py method">
  708. <dt class="sig sig-object py" id="pathlib.PurePath.joinpath">
  709. <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>
  710. <dd><p>Calling this method is equivalent to combining the path with each of
  711. the given <em>pathsegments</em> in turn:</p>
  712. <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>
  713. <span class="go">PurePosixPath(&#39;/etc/passwd&#39;)</span>
  714. <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>
  715. <span class="go">PurePosixPath(&#39;/etc/passwd&#39;)</span>
  716. <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>
  717. <span class="go">PurePosixPath(&#39;/etc/init.d/apache2&#39;)</span>
  718. <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>
  719. <span class="go">PureWindowsPath(&#39;c:/Program Files&#39;)</span>
  720. </pre></div>
  721. </div>
  722. </dd></dl>
  723. <dl class="py method">
  724. <dt class="sig sig-object py" id="pathlib.PurePath.match">
  725. <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="o"><span class="pre">*</span></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>
  726. <dd><p>Match this path against the provided glob-style pattern. Return <code class="docutils literal notranslate"><span class="pre">True</span></code>
  727. if matching is successful, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  728. <p>If <em>pattern</em> is relative, the path can be either relative or absolute,
  729. and matching is done from the right:</p>
  730. <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>
  731. <span class="go">True</span>
  732. <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>
  733. <span class="go">True</span>
  734. <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>
  735. <span class="go">False</span>
  736. </pre></div>
  737. </div>
  738. <p>If <em>pattern</em> is absolute, the path must be absolute, and the whole path
  739. must match:</p>
  740. <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.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>
  741. <span class="go">True</span>
  742. <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>
  743. <span class="go">False</span>
  744. </pre></div>
  745. </div>
  746. <p>The <em>pattern</em> may be another path object; this speeds up matching the same
  747. pattern against multiple files:</p>
  748. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pattern</span> <span class="o">=</span> <span class="n">PurePath</span><span class="p">(</span><span class="s1">&#39;*.py&#39;</span><span class="p">)</span>
  749. <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="n">pattern</span><span class="p">)</span>
  750. <span class="go">True</span>
  751. </pre></div>
  752. </div>
  753. <div class="versionchanged">
  754. <p><span class="versionmodified changed">Changed in version 3.12: </span>Accepts 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> interface.</p>
  755. </div>
  756. <p>As with other methods, case-sensitivity follows platform defaults:</p>
  757. <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">match</span><span class="p">(</span><span class="s1">&#39;*.PY&#39;</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;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>
  760. <span class="go">True</span>
  761. </pre></div>
  762. </div>
  763. <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>
  764. <div class="versionchanged">
  765. <p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>case_sensitive</em> parameter was added.</p>
  766. </div>
  767. </dd></dl>
  768. <dl class="py method">
  769. <dt class="sig sig-object py" id="pathlib.PurePath.relative_to">
  770. <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>
  771. <dd><p>Compute a version of this path relative to the path represented by
  772. <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>
  773. <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>
  774. <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>
  775. <span class="go">PurePosixPath(&#39;etc/passwd&#39;)</span>
  776. <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>
  777. <span class="go">PurePosixPath(&#39;passwd&#39;)</span>
  778. <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>
  779. <span class="gt">Traceback (most recent call last):</span>
  780. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  781. File <span class="nb">&quot;pathlib.py&quot;</span>, line <span class="m">941</span>, in <span class="n">relative_to</span>
  782. <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>
  783. <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>
  784. </pre></div>
  785. </div>
  786. <p>When <em>walk_up</em> is False (the default), the path must start with <em>other</em>.
  787. When the argument is True, <code class="docutils literal notranslate"><span class="pre">..</span></code> entries may be added to form the
  788. relative path. In all other cases, such as the paths referencing
  789. different 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>
  790. <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>
  791. <span class="go">PurePosixPath(&#39;../etc/passwd&#39;)</span>
  792. <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>
  793. <span class="gt">Traceback (most recent call last):</span>
  794. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  795. File <span class="nb">&quot;pathlib.py&quot;</span>, line <span class="m">941</span>, in <span class="n">relative_to</span>
  796. <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>
  797. <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>
  798. </pre></div>
  799. </div>
  800. <div class="admonition warning">
  801. <p class="admonition-title">Warning</p>
  802. <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.
  803. It does not check or access the underlying file structure.
  804. This can impact the <em>walk_up</em> option as it assumes that no symlinks
  805. are 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 if
  806. necessary to resolve symlinks.</p>
  807. </div>
  808. <div class="versionchanged">
  809. <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>
  810. </div>
  811. <div class="deprecated-removed">
  812. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>Passing additional positional arguments is deprecated; if supplied,
  813. they are joined with <em>other</em>.</p>
  814. </div>
  815. </dd></dl>
  816. <dl class="py method">
  817. <dt class="sig sig-object py" id="pathlib.PurePath.with_name">
  818. <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>
  819. <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 path
  820. doesn’t have a name, ValueError is raised:</p>
  821. <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>
  822. <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>
  823. <span class="go">PureWindowsPath(&#39;c:/Downloads/setup.py&#39;)</span>
  824. <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>
  825. <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>
  826. <span class="gt">Traceback (most recent call last):</span>
  827. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  828. 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>
  829. <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>
  830. <span class="gr">ValueError</span>: <span class="n">PureWindowsPath(&#39;c:/&#39;) has an empty name</span>
  831. </pre></div>
  832. </div>
  833. </dd></dl>
  834. <dl class="py method">
  835. <dt class="sig sig-object py" id="pathlib.PurePath.with_stem">
  836. <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>
  837. <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 path
  838. doesn’t have a name, ValueError is raised:</p>
  839. <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>
  840. <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>
  841. <span class="go">PureWindowsPath(&#39;c:/Downloads/final.txt&#39;)</span>
  842. <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>
  843. <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>
  844. <span class="go">PureWindowsPath(&#39;c:/Downloads/lib.gz&#39;)</span>
  845. <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>
  846. <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>
  847. <span class="gt">Traceback (most recent call last):</span>
  848. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  849. 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>
  850. <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>
  851. 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>
  852. <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>
  853. <span class="gr">ValueError</span>: <span class="n">PureWindowsPath(&#39;c:/&#39;) has an empty name</span>
  854. </pre></div>
  855. </div>
  856. <div class="versionadded">
  857. <p><span class="versionmodified added">New in version 3.9.</span></p>
  858. </div>
  859. </dd></dl>
  860. <dl class="py method">
  861. <dt class="sig sig-object py" id="pathlib.PurePath.with_suffix">
  862. <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>
  863. <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 path
  864. doesn’t have a suffix, the new <em>suffix</em> is appended instead. If the
  865. <em>suffix</em> is an empty string, the original suffix is removed:</p>
  866. <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>
  867. <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>
  868. <span class="go">PureWindowsPath(&#39;c:/Downloads/pathlib.tar.bz2&#39;)</span>
  869. <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>
  870. <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>
  871. <span class="go">PureWindowsPath(&#39;README.txt&#39;)</span>
  872. <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>
  873. <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>
  874. <span class="go">PureWindowsPath(&#39;README&#39;)</span>
  875. </pre></div>
  876. </div>
  877. </dd></dl>
  878. <dl class="py method">
  879. <dt class="sig sig-object py" id="pathlib.PurePath.with_segments">
  880. <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>
  881. <dd><p>Create a new path object of the same type by combining the given
  882. <em>pathsegments</em>. This method is called whenever a derivative path is created,
  883. such 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 may
  884. override this method to pass information to derivative paths, for example:</p>
  885. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">PurePosixPath</span>
  886. <span class="k">class</span> <span class="nc">MyPath</span><span class="p">(</span><span class="n">PurePosixPath</span><span class="p">):</span>
  887. <span class="k">def</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>
  888. <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>
  889. <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>
  890. <span class="k">def</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>
  891. <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>
  892. <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>
  893. <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>
  894. <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>
  895. </pre></div>
  896. </div>
  897. <div class="versionadded">
  898. <p><span class="versionmodified added">New in version 3.12.</span></p>
  899. </div>
  900. </dd></dl>
  901. </section>
  902. </section>
  903. <section id="concrete-paths">
  904. <span id="id2"></span><h2>Concrete paths<a class="headerlink" href="#concrete-paths" title="Link to this heading">¶</a></h2>
  905. <p>Concrete paths are subclasses of the pure path classes. In addition to
  906. operations provided by the latter, they also provide methods to do system
  907. calls on path objects. There are three ways to instantiate concrete paths:</p>
  908. <dl class="py class">
  909. <dt class="sig sig-object py" id="pathlib.Path">
  910. <em class="property"><span class="pre">class</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>
  911. <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 of
  912. the system’s path flavour (instantiating it creates either a
  913. <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>
  914. <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>
  915. <span class="go">PosixPath(&#39;setup.py&#39;)</span>
  916. </pre></div>
  917. </div>
  918. <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>
  919. </dd></dl>
  920. <dl class="py class">
  921. <dt class="sig sig-object py" id="pathlib.PosixPath">
  922. <em class="property"><span class="pre">class</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>
  923. <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 class
  924. represents concrete non-Windows filesystem paths:</p>
  925. <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&#39;</span><span class="p">)</span>
  926. <span class="go">PosixPath(&#39;/etc&#39;)</span>
  927. </pre></div>
  928. </div>
  929. <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>
  930. </dd></dl>
  931. <dl class="py class">
  932. <dt class="sig sig-object py" id="pathlib.WindowsPath">
  933. <em class="property"><span class="pre">class</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>
  934. <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 class
  935. represents concrete Windows filesystem paths:</p>
  936. <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:/Program Files/&#39;</span><span class="p">)</span>
  937. <span class="go">WindowsPath(&#39;c:/Program Files&#39;)</span>
  938. </pre></div>
  939. </div>
  940. <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>
  941. </dd></dl>
  942. <p>You can only instantiate the class flavour that corresponds to your system
  943. (allowing system calls on non-compatible path flavours could lead to
  944. bugs or failures in your application):</p>
  945. <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="nn">os</span>
  946. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">name</span>
  947. <span class="go">&#39;posix&#39;</span>
  948. <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>
  949. <span class="go">PosixPath(&#39;setup.py&#39;)</span>
  950. <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>
  951. <span class="go">PosixPath(&#39;setup.py&#39;)</span>
  952. <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>
  953. <span class="gt">Traceback (most recent call last):</span>
  954. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  955. File <span class="nb">&quot;pathlib.py&quot;</span>, line <span class="m">798</span>, in <span class="n">__new__</span>
  956. <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>
  957. <span class="gr">NotImplementedError</span>: <span class="n">cannot instantiate &#39;WindowsPath&#39; on your system</span>
  958. </pre></div>
  959. </div>
  960. <section id="methods">
  961. <h3>Methods<a class="headerlink" href="#methods" title="Link to this heading">¶</a></h3>
  962. <p>Concrete paths provide the following methods in addition to pure paths
  963. methods. Many of these 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
  964. call fails (for example because the path doesn’t exist).</p>
  965. <div class="versionchanged">
  966. <p><span class="versionmodified changed">Changed in version 3.8: </span><a class="reference internal" href="#pathlib.Path.exists" title="pathlib.Path.exists"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exists()</span></code></a>, <a class="reference internal" href="#pathlib.Path.is_dir" title="pathlib.Path.is_dir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_dir()</span></code></a>, <a class="reference internal" href="#pathlib.Path.is_file" title="pathlib.Path.is_file"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_file()</span></code></a>,
  967. <a class="reference internal" href="#pathlib.Path.is_mount" title="pathlib.Path.is_mount"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_mount()</span></code></a>, <a class="reference internal" href="#pathlib.Path.is_symlink" title="pathlib.Path.is_symlink"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_symlink()</span></code></a>,
  968. <a class="reference internal" href="#pathlib.Path.is_block_device" title="pathlib.Path.is_block_device"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_block_device()</span></code></a>, <a class="reference internal" href="#pathlib.Path.is_char_device" title="pathlib.Path.is_char_device"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_char_device()</span></code></a>,
  969. <a class="reference internal" href="#pathlib.Path.is_fifo" title="pathlib.Path.is_fifo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_fifo()</span></code></a>, <a class="reference internal" href="#pathlib.Path.is_socket" title="pathlib.Path.is_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_socket()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">False</span></code>
  970. instead of raising an exception for paths that contain characters
  971. unrepresentable at the OS level.</p>
  972. </div>
  973. <dl class="py method">
  974. <dt class="sig sig-object py" id="pathlib.Path.cwd">
  975. <em class="property"><span class="pre">classmethod</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>
  976. <dd><p>Return a new path object representing the current directory (as returned
  977. by <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>
  978. <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>
  979. <span class="go">PosixPath(&#39;/home/antoine/pathlib&#39;)</span>
  980. </pre></div>
  981. </div>
  982. </dd></dl>
  983. <dl class="py method">
  984. <dt class="sig sig-object py" id="pathlib.Path.home">
  985. <em class="property"><span class="pre">classmethod</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>
  986. <dd><p>Return a new path object representing the user’s home directory (as
  987. returned 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 home
  988. directory 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>
  989. <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>
  990. <span class="go">PosixPath(&#39;/home/antoine&#39;)</span>
  991. </pre></div>
  992. </div>
  993. <div class="versionadded">
  994. <p><span class="versionmodified added">New in version 3.5.</span></p>
  995. </div>
  996. </dd></dl>
  997. <dl class="py method">
  998. <dt class="sig sig-object py" id="pathlib.Path.stat">
  999. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">stat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.stat" title="Link to this definition">¶</a></dt>
  1000. <dd><p>Return a <a class="reference internal" href="os.html#os.stat_result" title="os.stat_result"><code class="xref py py-class docutils literal notranslate"><span class="pre">os.stat_result</span></code></a> object containing information about this path, like <a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a>.
  1001. The result is looked up at each call to this method.</p>
  1002. <p>This method normally follows symlinks; to stat a symlink add the argument
  1003. <code class="docutils literal notranslate"><span class="pre">follow_symlinks=False</span></code>, or use <a class="reference internal" href="#pathlib.Path.lstat" title="pathlib.Path.lstat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">lstat()</span></code></a>.</p>
  1004. <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;setup.py&#39;</span><span class="p">)</span>
  1005. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_size</span>
  1006. <span class="go">956</span>
  1007. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mtime</span>
  1008. <span class="go">1327883547.852554</span>
  1009. </pre></div>
  1010. </div>
  1011. <div class="versionchanged">
  1012. <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>follow_symlinks</em> parameter was added.</p>
  1013. </div>
  1014. </dd></dl>
  1015. <dl class="py method">
  1016. <dt class="sig sig-object py" id="pathlib.Path.chmod">
  1017. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">chmod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.chmod" title="Link to this definition">¶</a></dt>
  1018. <dd><p>Change the file mode and permissions, like <a class="reference internal" href="os.html#os.chmod" title="os.chmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.chmod()</span></code></a>.</p>
  1019. <p>This method normally follows symlinks. Some Unix flavours support changing
  1020. permissions on the symlink itself; on these platforms you may add the
  1021. argument <code class="docutils literal notranslate"><span class="pre">follow_symlinks=False</span></code>, or use <a class="reference internal" href="#pathlib.Path.lchmod" title="pathlib.Path.lchmod"><code class="xref py py-meth docutils literal notranslate"><span class="pre">lchmod()</span></code></a>.</p>
  1022. <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;setup.py&#39;</span><span class="p">)</span>
  1023. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mode</span>
  1024. <span class="go">33277</span>
  1025. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">chmod</span><span class="p">(</span><span class="mo">0o444</span><span class="p">)</span>
  1026. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_mode</span>
  1027. <span class="go">33060</span>
  1028. </pre></div>
  1029. </div>
  1030. <div class="versionchanged">
  1031. <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>follow_symlinks</em> parameter was added.</p>
  1032. </div>
  1033. </dd></dl>
  1034. <dl class="py method">
  1035. <dt class="sig sig-object py" id="pathlib.Path.exists">
  1036. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">exists</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.exists" title="Link to this definition">¶</a></dt>
  1037. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path points to an existing file or directory.</p>
  1038. <p>This method normally follows symlinks; to check if a symlink exists, add
  1039. the argument <code class="docutils literal notranslate"><span class="pre">follow_symlinks=False</span></code>.</p>
  1040. <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;.&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>
  1041. <span class="go">True</span>
  1042. <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><span class="o">.</span><span class="n">exists</span><span class="p">()</span>
  1043. <span class="go">True</span>
  1044. <span class="gp">&gt;&gt;&gt; </span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;/etc&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>
  1045. <span class="go">True</span>
  1046. <span class="gp">&gt;&gt;&gt; </span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;nonexistentfile&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>
  1047. <span class="go">False</span>
  1048. </pre></div>
  1049. </div>
  1050. <div class="versionchanged">
  1051. <p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>follow_symlinks</em> parameter was added.</p>
  1052. </div>
  1053. </dd></dl>
  1054. <dl class="py method">
  1055. <dt class="sig sig-object py" id="pathlib.Path.expanduser">
  1056. <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>
  1057. <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,
  1058. as 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 be
  1059. 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>
  1060. <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>
  1061. <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>
  1062. <span class="go">PosixPath(&#39;/home/eric/films/Monty Python&#39;)</span>
  1063. </pre></div>
  1064. </div>
  1065. <div class="versionadded">
  1066. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1067. </div>
  1068. </dd></dl>
  1069. <dl class="py method">
  1070. <dt class="sig sig-object py" id="pathlib.Path.glob">
  1071. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">glob</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="o"><span class="pre">*</span></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.Path.glob" title="Link to this definition">¶</a></dt>
  1072. <dd><p>Glob the given relative <em>pattern</em> in the directory represented by this path,
  1073. yielding all matching files (of any kind):</p>
  1074. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="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>
  1075. <span class="go">[PosixPath(&#39;pathlib.py&#39;), PosixPath(&#39;setup.py&#39;), PosixPath(&#39;test_pathlib.py&#39;)]</span>
  1076. <span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="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>
  1077. <span class="go">[PosixPath(&#39;docs/conf.py&#39;)]</span>
  1078. </pre></div>
  1079. </div>
  1080. <p>Patterns are the same as for <a class="reference internal" href="fnmatch.html#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><code class="xref py py-mod docutils literal notranslate"><span class="pre">fnmatch</span></code></a>, with the addition of “<code class="docutils literal notranslate"><span class="pre">**</span></code>”
  1081. which means “this directory and all subdirectories, recursively”. In other
  1082. words, it enables recursive globbing:</p>
  1083. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="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>
  1084. <span class="go">[PosixPath(&#39;build/lib/pathlib.py&#39;),</span>
  1085. <span class="go"> PosixPath(&#39;docs/conf.py&#39;),</span>
  1086. <span class="go"> PosixPath(&#39;pathlib.py&#39;),</span>
  1087. <span class="go"> PosixPath(&#39;setup.py&#39;),</span>
  1088. <span class="go"> PosixPath(&#39;test_pathlib.py&#39;)]</span>
  1089. </pre></div>
  1090. </div>
  1091. <p>This method calls <a class="reference internal" href="#pathlib.Path.is_dir" title="pathlib.Path.is_dir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.is_dir()</span></code></a> on the top-level directory and
  1092. propagates any <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> exception that is raised. Subsequent
  1093. <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> exceptions from scanning directories are suppressed.</p>
  1094. <p>By default, or when the <em>case_sensitive</em> keyword-only argument is set to
  1095. <code class="docutils literal notranslate"><span class="pre">None</span></code>, this method matches paths using platform-specific casing rules:
  1096. typically, case-sensitive on POSIX, and case-insensitive on Windows.
  1097. 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>
  1098. <div class="admonition note">
  1099. <p class="admonition-title">Note</p>
  1100. <p>Using the “<code class="docutils literal notranslate"><span class="pre">**</span></code>” pattern in large directory trees may consume
  1101. an inordinate amount of time.</p>
  1102. </div>
  1103. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">pathlib.Path.glob</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">self</span></code>, <code class="docutils literal notranslate"><span class="pre">pattern</span></code>.</p>
  1104. <div class="versionchanged">
  1105. <p><span class="versionmodified changed">Changed in version 3.11: </span>Return only directories if <em>pattern</em> ends with a pathname components
  1106. separator (<a class="reference internal" href="os.html#os.sep" title="os.sep"><code class="xref py py-data docutils literal notranslate"><span class="pre">sep</span></code></a> or <a class="reference internal" href="os.html#os.altsep" title="os.altsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">altsep</span></code></a>).</p>
  1107. </div>
  1108. <div class="versionchanged">
  1109. <p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>case_sensitive</em> parameter was added.</p>
  1110. </div>
  1111. </dd></dl>
  1112. <dl class="py method">
  1113. <dt class="sig sig-object py" id="pathlib.Path.group">
  1114. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">group</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.group" title="Link to this definition">¶</a></dt>
  1115. <dd><p>Return the name of the group owning the file. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> is raised
  1116. if the file’s gid isn’t found in the system database.</p>
  1117. </dd></dl>
  1118. <dl class="py method">
  1119. <dt class="sig sig-object py" id="pathlib.Path.is_dir">
  1120. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_dir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.is_dir" title="Link to this definition">¶</a></dt>
  1121. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path points to a directory (or a symbolic link
  1122. pointing to a directory), <code class="docutils literal notranslate"><span class="pre">False</span></code> if it points to another kind of file.</p>
  1123. <p><code class="docutils literal notranslate"><span class="pre">False</span></code> is also returned if the path doesn’t exist or is a broken symlink;
  1124. other errors (such as permission errors) are propagated.</p>
  1125. </dd></dl>
  1126. <dl class="py method">
  1127. <dt class="sig sig-object py" id="pathlib.Path.is_file">
  1128. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_file</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.is_file" title="Link to this definition">¶</a></dt>
  1129. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path points to a regular file (or a symbolic link
  1130. pointing to a regular file), <code class="docutils literal notranslate"><span class="pre">False</span></code> if it points to another kind of file.</p>
  1131. <p><code class="docutils literal notranslate"><span class="pre">False</span></code> is also returned if the path doesn’t exist or is a broken symlink;
  1132. other errors (such as permission errors) are propagated.</p>
  1133. </dd></dl>
  1134. <dl class="py method">
  1135. <dt class="sig sig-object py" id="pathlib.Path.is_junction">
  1136. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_junction</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.is_junction" title="Link to this definition">¶</a></dt>
  1137. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path points to a junction, and <code class="docutils literal notranslate"><span class="pre">False</span></code> for any other
  1138. type of file. Currently only Windows supports junctions.</p>
  1139. <div class="versionadded">
  1140. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1141. </div>
  1142. </dd></dl>
  1143. <dl class="py method">
  1144. <dt class="sig sig-object py" id="pathlib.Path.is_mount">
  1145. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_mount</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.is_mount" title="Link to this definition">¶</a></dt>
  1146. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path is a <em class="dfn">mount point</em>: a point in a
  1147. file system where a different file system has been mounted. On POSIX, the
  1148. function checks whether <em>path</em>’s parent, <code class="file docutils literal notranslate"><span class="pre">path/..</span></code>, is on a different
  1149. device than <em>path</em>, or whether <code class="file docutils literal notranslate"><span class="pre">path/..</span></code> and <em>path</em> point to the same
  1150. i-node on the same device — this should detect mount points for all Unix
  1151. and POSIX variants. On Windows, a mount point is considered to be a drive
  1152. letter root (e.g. <code class="docutils literal notranslate"><span class="pre">c:\</span></code>), a UNC share (e.g. <code class="docutils literal notranslate"><span class="pre">\\server\share</span></code>), or a
  1153. mounted filesystem directory.</p>
  1154. <div class="versionadded">
  1155. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1156. </div>
  1157. <div class="versionchanged">
  1158. <p><span class="versionmodified changed">Changed in version 3.12: </span>Windows support was added.</p>
  1159. </div>
  1160. </dd></dl>
  1161. <dl class="py method">
  1162. <dt class="sig sig-object py" id="pathlib.Path.is_symlink">
  1163. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_symlink</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.is_symlink" title="Link to this definition">¶</a></dt>
  1164. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path points to a symbolic link, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  1165. <p><code class="docutils literal notranslate"><span class="pre">False</span></code> is also returned if the path doesn’t exist; other errors (such
  1166. as permission errors) are propagated.</p>
  1167. </dd></dl>
  1168. <dl class="py method">
  1169. <dt class="sig sig-object py" id="pathlib.Path.is_socket">
  1170. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_socket</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.is_socket" title="Link to this definition">¶</a></dt>
  1171. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path points to a Unix socket (or a symbolic link
  1172. pointing to a Unix socket), <code class="docutils literal notranslate"><span class="pre">False</span></code> if it points to another kind of file.</p>
  1173. <p><code class="docutils literal notranslate"><span class="pre">False</span></code> is also returned if the path doesn’t exist or is a broken symlink;
  1174. other errors (such as permission errors) are propagated.</p>
  1175. </dd></dl>
  1176. <dl class="py method">
  1177. <dt class="sig sig-object py" id="pathlib.Path.is_fifo">
  1178. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_fifo</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.is_fifo" title="Link to this definition">¶</a></dt>
  1179. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path points to a FIFO (or a symbolic link
  1180. pointing to a FIFO), <code class="docutils literal notranslate"><span class="pre">False</span></code> if it points to another kind of file.</p>
  1181. <p><code class="docutils literal notranslate"><span class="pre">False</span></code> is also returned if the path doesn’t exist or is a broken symlink;
  1182. other errors (such as permission errors) are propagated.</p>
  1183. </dd></dl>
  1184. <dl class="py method">
  1185. <dt class="sig sig-object py" id="pathlib.Path.is_block_device">
  1186. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_block_device</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.is_block_device" title="Link to this definition">¶</a></dt>
  1187. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path points to a block device (or a symbolic link
  1188. pointing to a block device), <code class="docutils literal notranslate"><span class="pre">False</span></code> if it points to another kind of file.</p>
  1189. <p><code class="docutils literal notranslate"><span class="pre">False</span></code> is also returned if the path doesn’t exist or is a broken symlink;
  1190. other errors (such as permission errors) are propagated.</p>
  1191. </dd></dl>
  1192. <dl class="py method">
  1193. <dt class="sig sig-object py" id="pathlib.Path.is_char_device">
  1194. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">is_char_device</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.is_char_device" title="Link to this definition">¶</a></dt>
  1195. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the path points to a character device (or a symbolic link
  1196. pointing to a character device), <code class="docutils literal notranslate"><span class="pre">False</span></code> if it points to another kind of file.</p>
  1197. <p><code class="docutils literal notranslate"><span class="pre">False</span></code> is also returned if the path doesn’t exist or is a broken symlink;
  1198. other errors (such as permission errors) are propagated.</p>
  1199. </dd></dl>
  1200. <dl class="py method">
  1201. <dt class="sig sig-object py" id="pathlib.Path.iterdir">
  1202. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">iterdir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.iterdir" title="Link to this definition">¶</a></dt>
  1203. <dd><p>When the path points to a directory, yield path objects of the directory
  1204. contents:</p>
  1205. <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;docs&#39;</span><span class="p">)</span>
  1206. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">child</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="n">child</span>
  1207. <span class="gp">...</span>
  1208. <span class="go">PosixPath(&#39;docs/conf.py&#39;)</span>
  1209. <span class="go">PosixPath(&#39;docs/_templates&#39;)</span>
  1210. <span class="go">PosixPath(&#39;docs/make.bat&#39;)</span>
  1211. <span class="go">PosixPath(&#39;docs/index.rst&#39;)</span>
  1212. <span class="go">PosixPath(&#39;docs/_build&#39;)</span>
  1213. <span class="go">PosixPath(&#39;docs/_static&#39;)</span>
  1214. <span class="go">PosixPath(&#39;docs/Makefile&#39;)</span>
  1215. </pre></div>
  1216. </div>
  1217. <p>The children are yielded in arbitrary order, and the special entries
  1218. <code class="docutils literal notranslate"><span class="pre">'.'</span></code> and <code class="docutils literal notranslate"><span class="pre">'..'</span></code> are not included. If a file is removed from or added
  1219. to the directory after creating the iterator, whether a path object for
  1220. that file be included is unspecified.</p>
  1221. </dd></dl>
  1222. <dl class="py method">
  1223. <dt class="sig sig-object py" id="pathlib.Path.walk">
  1224. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">walk</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">top_down</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">on_error</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.walk" title="Link to this definition">¶</a></dt>
  1225. <dd><p>Generate the file names in a directory tree by walking the tree
  1226. either top-down or bottom-up.</p>
  1227. <p>For each directory in the directory tree rooted at <em>self</em> (including
  1228. <em>self</em> but excluding ‘.’ and ‘..’), the method yields a 3-tuple of
  1229. <code class="docutils literal notranslate"><span class="pre">(dirpath,</span> <span class="pre">dirnames,</span> <span class="pre">filenames)</span></code>.</p>
  1230. <p><em>dirpath</em> is a <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> to the directory currently being walked,
  1231. <em>dirnames</em> is a list of strings for the names of subdirectories in <em>dirpath</em>
  1232. (excluding <code class="docutils literal notranslate"><span class="pre">'.'</span></code> and <code class="docutils literal notranslate"><span class="pre">'..'</span></code>), and <em>filenames</em> is a list of strings for
  1233. the names of the non-directory files in <em>dirpath</em>. To get a full path
  1234. (which begins with <em>self</em>) to a file or directory in <em>dirpath</em>, do
  1235. <code class="docutils literal notranslate"><span class="pre">dirpath</span> <span class="pre">/</span> <span class="pre">name</span></code>. Whether or not the lists are sorted is file
  1236. system-dependent.</p>
  1237. <p>If the optional argument <em>top_down</em> is true (which is the default), the triple for a
  1238. directory is generated before the triples for any of its subdirectories
  1239. (directories are walked top-down). If <em>top_down</em> is false, the triple
  1240. for a directory is generated after the triples for all of its subdirectories
  1241. (directories are walked bottom-up). No matter the value of <em>top_down</em>, the
  1242. list of subdirectories is retrieved before the triples for the directory and
  1243. its subdirectories are walked.</p>
  1244. <p>When <em>top_down</em> is true, the caller can modify the <em>dirnames</em> list in-place
  1245. (for example, using <a class="reference internal" href="../reference/simple_stmts.html#del"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code></a> or slice assignment), and <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a>
  1246. will only recurse into the subdirectories whose names remain in <em>dirnames</em>.
  1247. This can be used to prune the search, or to impose a specific order of visiting,
  1248. or even to inform <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a> about directories the caller creates or
  1249. renames before it resumes <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a> again. Modifying <em>dirnames</em> when
  1250. <em>top_down</em> is false has no effect on the behavior of <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a> since the
  1251. directories in <em>dirnames</em> have already been generated by the time <em>dirnames</em>
  1252. is yielded to the caller.</p>
  1253. <p>By default, errors from <a class="reference internal" href="os.html#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.scandir()</span></code></a> are ignored. If the optional
  1254. argument <em>on_error</em> is specified, it should be a callable; it will be
  1255. called with one argument, 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> instance. The callable can handle the
  1256. error to continue the walk or re-raise it to stop the walk. Note that the
  1257. filename is available as the <code class="docutils literal notranslate"><span class="pre">filename</span></code> attribute of the exception object.</p>
  1258. <p>By default, <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a> does not follow symbolic links, and instead adds them
  1259. to the <em>filenames</em> list. Set <em>follow_symlinks</em> to true to resolve symlinks
  1260. and place them in <em>dirnames</em> and <em>filenames</em> as appropriate for their targets, and
  1261. consequently visit directories pointed to by symlinks (where supported).</p>
  1262. <div class="admonition note">
  1263. <p class="admonition-title">Note</p>
  1264. <p>Be aware that setting <em>follow_symlinks</em> to true can lead to infinite
  1265. recursion if a link points to a parent directory of itself. <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a>
  1266. does not keep track of the directories it has already visited.</p>
  1267. </div>
  1268. <div class="admonition note">
  1269. <p class="admonition-title">Note</p>
  1270. <p><a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a> assumes the directories it walks are not modified during
  1271. execution. For example, if a directory from <em>dirnames</em> has been replaced
  1272. with a symlink and <em>follow_symlinks</em> is false, <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a> will
  1273. still try to descend into it. To prevent such behavior, remove directories
  1274. from <em>dirnames</em> as appropriate.</p>
  1275. </div>
  1276. <div class="admonition note">
  1277. <p class="admonition-title">Note</p>
  1278. <p>Unlike <a class="reference internal" href="os.html#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.walk()</span></code></a>, <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a> lists symlinks to directories in
  1279. <em>filenames</em> if <em>follow_symlinks</em> is false.</p>
  1280. </div>
  1281. <p>This example displays the number of bytes used by all files in each directory,
  1282. while ignoring <code class="docutils literal notranslate"><span class="pre">__pycache__</span></code> directories:</p>
  1283. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
  1284. <span class="k">for</span> <span class="n">root</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">files</span> <span class="ow">in</span> <span class="n">Path</span><span class="p">(</span><span class="s2">&quot;cpython/Lib/concurrent&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">on_error</span><span class="o">=</span><span class="nb">print</span><span class="p">):</span>
  1285. <span class="nb">print</span><span class="p">(</span>
  1286. <span class="n">root</span><span class="p">,</span>
  1287. <span class="s2">&quot;consumes&quot;</span><span class="p">,</span>
  1288. <span class="nb">sum</span><span class="p">((</span><span class="n">root</span> <span class="o">/</span> <span class="n">file</span><span class="p">)</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_size</span> <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">files</span><span class="p">),</span>
  1289. <span class="s2">&quot;bytes in&quot;</span><span class="p">,</span>
  1290. <span class="nb">len</span><span class="p">(</span><span class="n">files</span><span class="p">),</span>
  1291. <span class="s2">&quot;non-directory files&quot;</span>
  1292. <span class="p">)</span>
  1293. <span class="k">if</span> <span class="s1">&#39;__pycache__&#39;</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span>
  1294. <span class="n">dirs</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&#39;__pycache__&#39;</span><span class="p">)</span>
  1295. </pre></div>
  1296. </div>
  1297. <p>This next example is a simple implementation of <a class="reference internal" href="shutil.html#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.rmtree()</span></code></a>.
  1298. Walking the tree bottom-up is essential as <a class="reference internal" href="#pathlib.Path.rmdir" title="pathlib.Path.rmdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmdir()</span></code></a> doesn’t allow
  1299. deleting a directory before it is empty:</p>
  1300. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Delete everything reachable from the directory &quot;top&quot;.</span>
  1301. <span class="c1"># CAUTION: This is dangerous! For example, if top == Path(&#39;/&#39;),</span>
  1302. <span class="c1"># it could delete all of your files.</span>
  1303. <span class="k">for</span> <span class="n">root</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">files</span> <span class="ow">in</span> <span class="n">top</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">top_down</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
  1304. <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
  1305. <span class="p">(</span><span class="n">root</span> <span class="o">/</span> <span class="n">name</span><span class="p">)</span><span class="o">.</span><span class="n">unlink</span><span class="p">()</span>
  1306. <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span>
  1307. <span class="p">(</span><span class="n">root</span> <span class="o">/</span> <span class="n">name</span><span class="p">)</span><span class="o">.</span><span class="n">rmdir</span><span class="p">()</span>
  1308. </pre></div>
  1309. </div>
  1310. <div class="versionadded">
  1311. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1312. </div>
  1313. </dd></dl>
  1314. <dl class="py method">
  1315. <dt class="sig sig-object py" id="pathlib.Path.lchmod">
  1316. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">lchmod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.lchmod" title="Link to this definition">¶</a></dt>
  1317. <dd><p>Like <a class="reference internal" href="#pathlib.Path.chmod" title="pathlib.Path.chmod"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.chmod()</span></code></a> but, if the path points to a symbolic link, the
  1318. symbolic link’s mode is changed rather than its target’s.</p>
  1319. </dd></dl>
  1320. <dl class="py method">
  1321. <dt class="sig sig-object py" id="pathlib.Path.lstat">
  1322. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">lstat</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.lstat" title="Link to this definition">¶</a></dt>
  1323. <dd><p>Like <a class="reference internal" href="#pathlib.Path.stat" title="pathlib.Path.stat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.stat()</span></code></a> but, if the path points to a symbolic link, return
  1324. the symbolic link’s information rather than its target’s.</p>
  1325. </dd></dl>
  1326. <dl class="py method">
  1327. <dt class="sig sig-object py" id="pathlib.Path.mkdir">
  1328. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">mkdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0o777</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parents</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exist_ok</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.mkdir" title="Link to this definition">¶</a></dt>
  1329. <dd><p>Create a new directory at this given path. If <em>mode</em> is given, it is
  1330. combined with the process’ <code class="docutils literal notranslate"><span class="pre">umask</span></code> value to determine the file mode
  1331. and access flags. If the path already exists, <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a>
  1332. is raised.</p>
  1333. <p>If <em>parents</em> is true, any missing parents of this path are created
  1334. as needed; they are created with the default permissions without taking
  1335. <em>mode</em> into account (mimicking the POSIX <code class="docutils literal notranslate"><span class="pre">mkdir</span> <span class="pre">-p</span></code> command).</p>
  1336. <p>If <em>parents</em> is false (the default), a missing parent raises
  1337. <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a>.</p>
  1338. <p>If <em>exist_ok</em> is false (the default), <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> is
  1339. raised if the target directory already exists.</p>
  1340. <p>If <em>exist_ok</em> is true, <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> will not be raised unless the given
  1341. path already exists in the file system and is not a directory (same
  1342. behavior as the POSIX <code class="docutils literal notranslate"><span class="pre">mkdir</span> <span class="pre">-p</span></code> command).</p>
  1343. <div class="versionchanged">
  1344. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <em>exist_ok</em> parameter was added.</p>
  1345. </div>
  1346. </dd></dl>
  1347. <dl class="py method">
  1348. <dt class="sig sig-object py" id="pathlib.Path.open">
  1349. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffering</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</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.Path.open" title="Link to this definition">¶</a></dt>
  1350. <dd><p>Open the file pointed to by the path, like the built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>
  1351. function does:</p>
  1352. <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;setup.py&#39;</span><span class="p">)</span>
  1353. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">p</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>
  1354. <span class="gp">... </span> <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
  1355. <span class="gp">...</span>
  1356. <span class="go">&#39;#!/usr/bin/env python3\n&#39;</span>
  1357. </pre></div>
  1358. </div>
  1359. </dd></dl>
  1360. <dl class="py method">
  1361. <dt class="sig sig-object py" id="pathlib.Path.owner">
  1362. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">owner</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.owner" title="Link to this definition">¶</a></dt>
  1363. <dd><p>Return the name of the user owning the file. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> is raised
  1364. if the file’s uid isn’t found in the system database.</p>
  1365. </dd></dl>
  1366. <dl class="py method">
  1367. <dt class="sig sig-object py" id="pathlib.Path.read_bytes">
  1368. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">read_bytes</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.read_bytes" title="Link to this definition">¶</a></dt>
  1369. <dd><p>Return the binary contents of the pointed-to file as a bytes object:</p>
  1370. <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;my_binary_file&#39;</span><span class="p">)</span>
  1371. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">write_bytes</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Binary file contents&#39;</span><span class="p">)</span>
  1372. <span class="go">20</span>
  1373. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">read_bytes</span><span class="p">()</span>
  1374. <span class="go">b&#39;Binary file contents&#39;</span>
  1375. </pre></div>
  1376. </div>
  1377. <div class="versionadded">
  1378. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1379. </div>
  1380. </dd></dl>
  1381. <dl class="py method">
  1382. <dt class="sig sig-object py" id="pathlib.Path.read_text">
  1383. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">read_text</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.read_text" title="Link to this definition">¶</a></dt>
  1384. <dd><p>Return the decoded contents of the pointed-to file as a string:</p>
  1385. <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;my_text_file&#39;</span><span class="p">)</span>
  1386. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">write_text</span><span class="p">(</span><span class="s1">&#39;Text file contents&#39;</span><span class="p">)</span>
  1387. <span class="go">18</span>
  1388. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">read_text</span><span class="p">()</span>
  1389. <span class="go">&#39;Text file contents&#39;</span>
  1390. </pre></div>
  1391. </div>
  1392. <p>The file is opened and then closed. The optional parameters have the same
  1393. meaning as in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>.</p>
  1394. <div class="versionadded">
  1395. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1396. </div>
  1397. </dd></dl>
  1398. <dl class="py method">
  1399. <dt class="sig sig-object py" id="pathlib.Path.readlink">
  1400. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">readlink</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.readlink" title="Link to this definition">¶</a></dt>
  1401. <dd><p>Return the path to which the symbolic link points (as returned by
  1402. <a class="reference internal" href="os.html#os.readlink" title="os.readlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.readlink()</span></code></a>):</p>
  1403. <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;mylink&#39;</span><span class="p">)</span>
  1404. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">symlink_to</span><span class="p">(</span><span class="s1">&#39;setup.py&#39;</span><span class="p">)</span>
  1405. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">readlink</span><span class="p">()</span>
  1406. <span class="go">PosixPath(&#39;setup.py&#39;)</span>
  1407. </pre></div>
  1408. </div>
  1409. <div class="versionadded">
  1410. <p><span class="versionmodified added">New in version 3.9.</span></p>
  1411. </div>
  1412. </dd></dl>
  1413. <dl class="py method">
  1414. <dt class="sig sig-object py" id="pathlib.Path.rename">
  1415. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">rename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">target</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.rename" title="Link to this definition">¶</a></dt>
  1416. <dd><p>Rename this file or directory to the given <em>target</em>, and return a new Path
  1417. instance pointing to <em>target</em>. On Unix, if <em>target</em> exists and is a file,
  1418. it will be replaced silently if the user has permission.
  1419. On Windows, if <em>target</em> exists, <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> will be raised.
  1420. <em>target</em> can be either a string or another path object:</p>
  1421. <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;foo&#39;</span><span class="p">)</span>
  1422. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;w&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;some text&#39;</span><span class="p">)</span>
  1423. <span class="go">9</span>
  1424. <span class="gp">&gt;&gt;&gt; </span><span class="n">target</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="s1">&#39;bar&#39;</span><span class="p">)</span>
  1425. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">target</span><span class="p">)</span>
  1426. <span class="go">PosixPath(&#39;bar&#39;)</span>
  1427. <span class="gp">&gt;&gt;&gt; </span><span class="n">target</span><span class="o">.</span><span class="n">open</span><span class="p">()</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  1428. <span class="go">&#39;some text&#39;</span>
  1429. </pre></div>
  1430. </div>
  1431. <p>The target path may be absolute or relative. Relative paths are interpreted
  1432. relative to the current working directory, <em>not</em> the directory of the Path
  1433. object.</p>
  1434. <p>It is implemented in terms of <a class="reference internal" href="os.html#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rename()</span></code></a> and gives the same guarantees.</p>
  1435. <div class="versionchanged">
  1436. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added return value, return the new Path instance.</p>
  1437. </div>
  1438. </dd></dl>
  1439. <dl class="py method">
  1440. <dt class="sig sig-object py" id="pathlib.Path.replace">
  1441. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">replace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">target</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.replace" title="Link to this definition">¶</a></dt>
  1442. <dd><p>Rename this file or directory to the given <em>target</em>, and return a new Path
  1443. instance pointing to <em>target</em>. If <em>target</em> points to an existing file or
  1444. empty directory, it will be unconditionally replaced.</p>
  1445. <p>The target path may be absolute or relative. Relative paths are interpreted
  1446. relative to the current working directory, <em>not</em> the directory of the Path
  1447. object.</p>
  1448. <div class="versionchanged">
  1449. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added return value, return the new Path instance.</p>
  1450. </div>
  1451. </dd></dl>
  1452. <dl class="py method">
  1453. <dt class="sig sig-object py" id="pathlib.Path.absolute">
  1454. <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>
  1455. <dd><p>Make the path absolute, without normalization or resolving symlinks.
  1456. Returns a new path object:</p>
  1457. <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;tests&#39;</span><span class="p">)</span>
  1458. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span>
  1459. <span class="go">PosixPath(&#39;tests&#39;)</span>
  1460. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">absolute</span><span class="p">()</span>
  1461. <span class="go">PosixPath(&#39;/home/antoine/pathlib/tests&#39;)</span>
  1462. </pre></div>
  1463. </div>
  1464. </dd></dl>
  1465. <dl class="py method">
  1466. <dt class="sig sig-object py" id="pathlib.Path.resolve">
  1467. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">resolve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">strict</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.Path.resolve" title="Link to this definition">¶</a></dt>
  1468. <dd><p>Make the path absolute, resolving any symlinks. A new path object is
  1469. returned:</p>
  1470. <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>
  1471. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span>
  1472. <span class="go">PosixPath(&#39;.&#39;)</span>
  1473. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">resolve</span><span class="p">()</span>
  1474. <span class="go">PosixPath(&#39;/home/antoine/pathlib&#39;)</span>
  1475. </pre></div>
  1476. </div>
  1477. <p>“<code class="docutils literal notranslate"><span class="pre">..</span></code>” components are also eliminated (this is the only method to do so):</p>
  1478. <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;docs/../setup.py&#39;</span><span class="p">)</span>
  1479. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">resolve</span><span class="p">()</span>
  1480. <span class="go">PosixPath(&#39;/home/antoine/pathlib/setup.py&#39;)</span>
  1481. </pre></div>
  1482. </div>
  1483. <p>If the path doesn’t exist and <em>strict</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a>
  1484. is raised. If <em>strict</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, the path is resolved as far as possible
  1485. and any remainder is appended without checking whether it exists. If an
  1486. infinite loop is encountered along the resolution path, <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>
  1487. is raised.</p>
  1488. <div class="versionchanged">
  1489. <p><span class="versionmodified changed">Changed in version 3.6: </span>The <em>strict</em> parameter was added (pre-3.6 behavior is strict).</p>
  1490. </div>
  1491. </dd></dl>
  1492. <dl class="py method">
  1493. <dt class="sig sig-object py" id="pathlib.Path.rglob">
  1494. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">rglob</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="o"><span class="pre">*</span></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.Path.rglob" title="Link to this definition">¶</a></dt>
  1495. <dd><p>Glob the given relative <em>pattern</em> recursively. This is like calling
  1496. <a class="reference internal" href="#pathlib.Path.glob" title="pathlib.Path.glob"><code class="xref py py-func docutils literal notranslate"><span class="pre">Path.glob()</span></code></a> with “<code class="docutils literal notranslate"><span class="pre">**/</span></code>” added in front of the <em>pattern</em>, where
  1497. <em>patterns</em> are the same as for <a class="reference internal" href="fnmatch.html#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><code class="xref py py-mod docutils literal notranslate"><span class="pre">fnmatch</span></code></a>:</p>
  1498. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">Path</span><span class="p">()</span><span class="o">.</span><span class="n">rglob</span><span class="p">(</span><span class="s2">&quot;*.py&quot;</span><span class="p">))</span>
  1499. <span class="go">[PosixPath(&#39;build/lib/pathlib.py&#39;),</span>
  1500. <span class="go"> PosixPath(&#39;docs/conf.py&#39;),</span>
  1501. <span class="go"> PosixPath(&#39;pathlib.py&#39;),</span>
  1502. <span class="go"> PosixPath(&#39;setup.py&#39;),</span>
  1503. <span class="go"> PosixPath(&#39;test_pathlib.py&#39;)]</span>
  1504. </pre></div>
  1505. </div>
  1506. <p>By default, or when the <em>case_sensitive</em> keyword-only argument is set to
  1507. <code class="docutils literal notranslate"><span class="pre">None</span></code>, this method matches paths using platform-specific casing rules:
  1508. typically, case-sensitive on POSIX, and case-insensitive on Windows.
  1509. 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>
  1510. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">pathlib.Path.rglob</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">self</span></code>, <code class="docutils literal notranslate"><span class="pre">pattern</span></code>.</p>
  1511. <div class="versionchanged">
  1512. <p><span class="versionmodified changed">Changed in version 3.11: </span>Return only directories if <em>pattern</em> ends with a pathname components
  1513. separator (<a class="reference internal" href="os.html#os.sep" title="os.sep"><code class="xref py py-data docutils literal notranslate"><span class="pre">sep</span></code></a> or <a class="reference internal" href="os.html#os.altsep" title="os.altsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">altsep</span></code></a>).</p>
  1514. </div>
  1515. <div class="versionchanged">
  1516. <p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>case_sensitive</em> parameter was added.</p>
  1517. </div>
  1518. </dd></dl>
  1519. <dl class="py method">
  1520. <dt class="sig sig-object py" id="pathlib.Path.rmdir">
  1521. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">rmdir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.rmdir" title="Link to this definition">¶</a></dt>
  1522. <dd><p>Remove this directory. The directory must be empty.</p>
  1523. </dd></dl>
  1524. <dl class="py method">
  1525. <dt class="sig sig-object py" id="pathlib.Path.samefile">
  1526. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">samefile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other_path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.samefile" title="Link to this definition">¶</a></dt>
  1527. <dd><p>Return whether this path points to the same file as <em>other_path</em>, which
  1528. can be either a Path object, or a string. The semantics are similar
  1529. to <a class="reference internal" href="os.path.html#os.path.samefile" title="os.path.samefile"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.samefile()</span></code></a> and <a class="reference internal" href="os.path.html#os.path.samestat" title="os.path.samestat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.samestat()</span></code></a>.</p>
  1530. <p>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> can be raised if either file cannot be accessed for some
  1531. reason.</p>
  1532. <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;spam&#39;</span><span class="p">)</span>
  1533. <span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="s1">&#39;eggs&#39;</span><span class="p">)</span>
  1534. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">samefile</span><span class="p">(</span><span class="n">q</span><span class="p">)</span>
  1535. <span class="go">False</span>
  1536. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">samefile</span><span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">)</span>
  1537. <span class="go">True</span>
  1538. </pre></div>
  1539. </div>
  1540. <div class="versionadded">
  1541. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1542. </div>
  1543. </dd></dl>
  1544. <dl class="py method">
  1545. <dt class="sig sig-object py" id="pathlib.Path.symlink_to">
  1546. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">symlink_to</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">target</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target_is_directory</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.Path.symlink_to" title="Link to this definition">¶</a></dt>
  1547. <dd><p>Make this path a symbolic link pointing to <em>target</em>.</p>
  1548. <p>On Windows, a symlink represents either a file or a directory, and does not
  1549. morph to the target dynamically. If the target is present, the type of the
  1550. symlink will be created to match. Otherwise, the symlink will be created
  1551. as a directory if <em>target_is_directory</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> or a file symlink (the
  1552. default) otherwise. On non-Windows platforms, <em>target_is_directory</em> is ignored.</p>
  1553. <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;mylink&#39;</span><span class="p">)</span>
  1554. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">symlink_to</span><span class="p">(</span><span class="s1">&#39;setup.py&#39;</span><span class="p">)</span>
  1555. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">resolve</span><span class="p">()</span>
  1556. <span class="go">PosixPath(&#39;/home/antoine/pathlib/setup.py&#39;)</span>
  1557. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span><span class="o">.</span><span class="n">st_size</span>
  1558. <span class="go">956</span>
  1559. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">lstat</span><span class="p">()</span><span class="o">.</span><span class="n">st_size</span>
  1560. <span class="go">8</span>
  1561. </pre></div>
  1562. </div>
  1563. <div class="admonition note">
  1564. <p class="admonition-title">Note</p>
  1565. <p>The order of arguments (link, target) is the reverse
  1566. of <a class="reference internal" href="os.html#os.symlink" title="os.symlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.symlink()</span></code></a>’s.</p>
  1567. </div>
  1568. </dd></dl>
  1569. <dl class="py method">
  1570. <dt class="sig sig-object py" id="pathlib.Path.hardlink_to">
  1571. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">hardlink_to</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">target</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.hardlink_to" title="Link to this definition">¶</a></dt>
  1572. <dd><p>Make this path a hard link to the same file as <em>target</em>.</p>
  1573. <div class="admonition note">
  1574. <p class="admonition-title">Note</p>
  1575. <p>The order of arguments (link, target) is the reverse
  1576. of <a class="reference internal" href="os.html#os.link" title="os.link"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.link()</span></code></a>’s.</p>
  1577. </div>
  1578. <div class="versionadded">
  1579. <p><span class="versionmodified added">New in version 3.10.</span></p>
  1580. </div>
  1581. </dd></dl>
  1582. <dl class="py method">
  1583. <dt class="sig sig-object py" id="pathlib.Path.touch">
  1584. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">touch</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0o666</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exist_ok</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.touch" title="Link to this definition">¶</a></dt>
  1585. <dd><p>Create a file at this given path. If <em>mode</em> is given, it is combined
  1586. with the process’ <code class="docutils literal notranslate"><span class="pre">umask</span></code> value to determine the file mode and access
  1587. flags. If the file already exists, the function succeeds if <em>exist_ok</em>
  1588. is true (and its modification time is updated to the current time),
  1589. otherwise <a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> is raised.</p>
  1590. </dd></dl>
  1591. <dl class="py method">
  1592. <dt class="sig sig-object py" id="pathlib.Path.unlink">
  1593. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">unlink</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">missing_ok</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.unlink" title="Link to this definition">¶</a></dt>
  1594. <dd><p>Remove this file or symbolic link. If the path points to a directory,
  1595. use <a class="reference internal" href="#pathlib.Path.rmdir" title="pathlib.Path.rmdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">Path.rmdir()</span></code></a> instead.</p>
  1596. <p>If <em>missing_ok</em> is false (the default), <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> is
  1597. raised if the path does not exist.</p>
  1598. <p>If <em>missing_ok</em> is true, <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> exceptions will be
  1599. ignored (same behavior as the POSIX <code class="docutils literal notranslate"><span class="pre">rm</span> <span class="pre">-f</span></code> command).</p>
  1600. <div class="versionchanged">
  1601. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>missing_ok</em> parameter was added.</p>
  1602. </div>
  1603. </dd></dl>
  1604. <dl class="py method">
  1605. <dt class="sig sig-object py" id="pathlib.Path.write_bytes">
  1606. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">write_bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pathlib.Path.write_bytes" title="Link to this definition">¶</a></dt>
  1607. <dd><p>Open the file pointed to in bytes mode, write <em>data</em> to it, and close the
  1608. file:</p>
  1609. <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;my_binary_file&#39;</span><span class="p">)</span>
  1610. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">write_bytes</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Binary file contents&#39;</span><span class="p">)</span>
  1611. <span class="go">20</span>
  1612. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">read_bytes</span><span class="p">()</span>
  1613. <span class="go">b&#39;Binary file contents&#39;</span>
  1614. </pre></div>
  1615. </div>
  1616. <p>An existing file of the same name is overwritten.</p>
  1617. <div class="versionadded">
  1618. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1619. </div>
  1620. </dd></dl>
  1621. <dl class="py method">
  1622. <dt class="sig sig-object py" id="pathlib.Path.write_text">
  1623. <span class="sig-prename descclassname"><span class="pre">Path.</span></span><span class="sig-name descname"><span class="pre">write_text</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</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.Path.write_text" title="Link to this definition">¶</a></dt>
  1624. <dd><p>Open the file pointed to in text mode, write <em>data</em> to it, and close the
  1625. file:</p>
  1626. <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;my_text_file&#39;</span><span class="p">)</span>
  1627. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">write_text</span><span class="p">(</span><span class="s1">&#39;Text file contents&#39;</span><span class="p">)</span>
  1628. <span class="go">18</span>
  1629. <span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">read_text</span><span class="p">()</span>
  1630. <span class="go">&#39;Text file contents&#39;</span>
  1631. </pre></div>
  1632. </div>
  1633. <p>An existing file of the same name is overwritten. The optional parameters
  1634. have the same meaning as in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>.</p>
  1635. <div class="versionadded">
  1636. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1637. </div>
  1638. <div class="versionchanged">
  1639. <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>newline</em> parameter was added.</p>
  1640. </div>
  1641. </dd></dl>
  1642. </section>
  1643. </section>
  1644. <section id="correspondence-to-tools-in-the-os-module">
  1645. <h2>Correspondence to tools in the <a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module<a class="headerlink" href="#correspondence-to-tools-in-the-os-module" title="Link to this heading">¶</a></h2>
  1646. <p>Below is a table mapping various <a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> functions to their corresponding
  1647. <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>/<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> equivalent.</p>
  1648. <div class="admonition note">
  1649. <p class="admonition-title">Note</p>
  1650. <p>Not all pairs of functions/methods below are equivalent. Some of them,
  1651. despite having some overlapping use-cases, have different semantics. They
  1652. include <a class="reference internal" href="os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.abspath()</span></code></a> and <a class="reference internal" href="#pathlib.Path.absolute" title="pathlib.Path.absolute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.absolute()</span></code></a>,
  1653. <a class="reference internal" href="os.path.html#os.path.relpath" title="os.path.relpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.relpath()</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">PurePath.relative_to()</span></code></a>.</p>
  1654. </div>
  1655. <table class="docutils align-default">
  1656. <thead>
  1657. <tr class="row-odd"><th class="head"><p><a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> and <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></p></th>
  1658. <th class="head"><p><a class="reference internal" href="#module-pathlib" title="pathlib: Object-oriented filesystem paths"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code></a></p></th>
  1659. </tr>
  1660. </thead>
  1661. <tbody>
  1662. <tr class="row-even"><td><p><a class="reference internal" href="os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.abspath()</span></code></a></p></td>
  1663. <td><p><a class="reference internal" href="#pathlib.Path.absolute" title="pathlib.Path.absolute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.absolute()</span></code></a> <a class="footnote-reference brackets" href="#id5" id="id3" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  1664. </tr>
  1665. <tr class="row-odd"><td><p><a class="reference internal" href="os.path.html#os.path.realpath" title="os.path.realpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.realpath()</span></code></a></p></td>
  1666. <td><p><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></p></td>
  1667. </tr>
  1668. <tr class="row-even"><td><p><a class="reference internal" href="os.html#os.chmod" title="os.chmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.chmod()</span></code></a></p></td>
  1669. <td><p><a class="reference internal" href="#pathlib.Path.chmod" title="pathlib.Path.chmod"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.chmod()</span></code></a></p></td>
  1670. </tr>
  1671. <tr class="row-odd"><td><p><a class="reference internal" href="os.html#os.mkdir" title="os.mkdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.mkdir()</span></code></a></p></td>
  1672. <td><p><a class="reference internal" href="#pathlib.Path.mkdir" title="pathlib.Path.mkdir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.mkdir()</span></code></a></p></td>
  1673. </tr>
  1674. <tr class="row-even"><td><p><a class="reference internal" href="os.html#os.makedirs" title="os.makedirs"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.makedirs()</span></code></a></p></td>
  1675. <td><p><a class="reference internal" href="#pathlib.Path.mkdir" title="pathlib.Path.mkdir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.mkdir()</span></code></a></p></td>
  1676. </tr>
  1677. <tr class="row-odd"><td><p><a class="reference internal" href="os.html#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rename()</span></code></a></p></td>
  1678. <td><p><a class="reference internal" href="#pathlib.Path.rename" title="pathlib.Path.rename"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.rename()</span></code></a></p></td>
  1679. </tr>
  1680. <tr class="row-even"><td><p><a class="reference internal" href="os.html#os.replace" title="os.replace"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.replace()</span></code></a></p></td>
  1681. <td><p><a class="reference internal" href="#pathlib.Path.replace" title="pathlib.Path.replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.replace()</span></code></a></p></td>
  1682. </tr>
  1683. <tr class="row-odd"><td><p><a class="reference internal" href="os.html#os.rmdir" title="os.rmdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rmdir()</span></code></a></p></td>
  1684. <td><p><a class="reference internal" href="#pathlib.Path.rmdir" title="pathlib.Path.rmdir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.rmdir()</span></code></a></p></td>
  1685. </tr>
  1686. <tr class="row-even"><td><p><a class="reference internal" href="os.html#os.remove" title="os.remove"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.remove()</span></code></a>, <a class="reference internal" href="os.html#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.unlink()</span></code></a></p></td>
  1687. <td><p><a class="reference internal" href="#pathlib.Path.unlink" title="pathlib.Path.unlink"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.unlink()</span></code></a></p></td>
  1688. </tr>
  1689. <tr class="row-odd"><td><p><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></td>
  1690. <td><p><a class="reference internal" href="#pathlib.Path.cwd" title="pathlib.Path.cwd"><code class="xref py py-func docutils literal notranslate"><span class="pre">Path.cwd()</span></code></a></p></td>
  1691. </tr>
  1692. <tr class="row-even"><td><p><a class="reference internal" href="os.path.html#os.path.exists" title="os.path.exists"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.exists()</span></code></a></p></td>
  1693. <td><p><a class="reference internal" href="#pathlib.Path.exists" title="pathlib.Path.exists"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.exists()</span></code></a></p></td>
  1694. </tr>
  1695. <tr class="row-odd"><td><p><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></p></td>
  1696. <td><p><a class="reference internal" href="#pathlib.Path.expanduser" title="pathlib.Path.expanduser"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.expanduser()</span></code></a> and
  1697. <a class="reference internal" href="#pathlib.Path.home" title="pathlib.Path.home"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.home()</span></code></a></p></td>
  1698. </tr>
  1699. <tr class="row-even"><td><p><a class="reference internal" href="os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a></p></td>
  1700. <td><p><a class="reference internal" href="#pathlib.Path.iterdir" title="pathlib.Path.iterdir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.iterdir()</span></code></a></p></td>
  1701. </tr>
  1702. <tr class="row-odd"><td><p><a class="reference internal" href="os.html#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.walk()</span></code></a></p></td>
  1703. <td><p><a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.walk()</span></code></a></p></td>
  1704. </tr>
  1705. <tr class="row-even"><td><p><a class="reference internal" href="os.path.html#os.path.isdir" title="os.path.isdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.isdir()</span></code></a></p></td>
  1706. <td><p><a class="reference internal" href="#pathlib.Path.is_dir" title="pathlib.Path.is_dir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.is_dir()</span></code></a></p></td>
  1707. </tr>
  1708. <tr class="row-odd"><td><p><a class="reference internal" href="os.path.html#os.path.isfile" title="os.path.isfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.isfile()</span></code></a></p></td>
  1709. <td><p><a class="reference internal" href="#pathlib.Path.is_file" title="pathlib.Path.is_file"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.is_file()</span></code></a></p></td>
  1710. </tr>
  1711. <tr class="row-even"><td><p><a class="reference internal" href="os.path.html#os.path.islink" title="os.path.islink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.islink()</span></code></a></p></td>
  1712. <td><p><a class="reference internal" href="#pathlib.Path.is_symlink" title="pathlib.Path.is_symlink"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.is_symlink()</span></code></a></p></td>
  1713. </tr>
  1714. <tr class="row-odd"><td><p><a class="reference internal" href="os.html#os.link" title="os.link"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.link()</span></code></a></p></td>
  1715. <td><p><a class="reference internal" href="#pathlib.Path.hardlink_to" title="pathlib.Path.hardlink_to"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.hardlink_to()</span></code></a></p></td>
  1716. </tr>
  1717. <tr class="row-even"><td><p><a class="reference internal" href="os.html#os.symlink" title="os.symlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.symlink()</span></code></a></p></td>
  1718. <td><p><a class="reference internal" href="#pathlib.Path.symlink_to" title="pathlib.Path.symlink_to"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.symlink_to()</span></code></a></p></td>
  1719. </tr>
  1720. <tr class="row-odd"><td><p><a class="reference internal" href="os.html#os.readlink" title="os.readlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.readlink()</span></code></a></p></td>
  1721. <td><p><a class="reference internal" href="#pathlib.Path.readlink" title="pathlib.Path.readlink"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.readlink()</span></code></a></p></td>
  1722. </tr>
  1723. <tr class="row-even"><td><p><a class="reference internal" href="os.path.html#os.path.relpath" title="os.path.relpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.relpath()</span></code></a></p></td>
  1724. <td><p><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">PurePath.relative_to()</span></code></a> <a class="footnote-reference brackets" href="#id6" id="id4" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p></td>
  1725. </tr>
  1726. <tr class="row-odd"><td><p><a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a></p></td>
  1727. <td><p><a class="reference internal" href="#pathlib.Path.stat" title="pathlib.Path.stat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.stat()</span></code></a>,
  1728. <a class="reference internal" href="#pathlib.Path.owner" title="pathlib.Path.owner"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.owner()</span></code></a>,
  1729. <a class="reference internal" href="#pathlib.Path.group" title="pathlib.Path.group"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.group()</span></code></a></p></td>
  1730. </tr>
  1731. <tr class="row-even"><td><p><a class="reference internal" href="os.path.html#os.path.isabs" title="os.path.isabs"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.isabs()</span></code></a></p></td>
  1732. <td><p><a class="reference internal" href="#pathlib.PurePath.is_absolute" title="pathlib.PurePath.is_absolute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">PurePath.is_absolute()</span></code></a></p></td>
  1733. </tr>
  1734. <tr class="row-odd"><td><p><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></td>
  1735. <td><p><a class="reference internal" href="#pathlib.PurePath.joinpath" title="pathlib.PurePath.joinpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">PurePath.joinpath()</span></code></a></p></td>
  1736. </tr>
  1737. <tr class="row-even"><td><p><a class="reference internal" href="os.path.html#os.path.basename" title="os.path.basename"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.basename()</span></code></a></p></td>
  1738. <td><p><a class="reference internal" href="#pathlib.PurePath.name" title="pathlib.PurePath.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PurePath.name</span></code></a></p></td>
  1739. </tr>
  1740. <tr class="row-odd"><td><p><a class="reference internal" href="os.path.html#os.path.dirname" title="os.path.dirname"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.dirname()</span></code></a></p></td>
  1741. <td><p><a class="reference internal" href="#pathlib.PurePath.parent" title="pathlib.PurePath.parent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PurePath.parent</span></code></a></p></td>
  1742. </tr>
  1743. <tr class="row-even"><td><p><a class="reference internal" href="os.path.html#os.path.samefile" title="os.path.samefile"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.samefile()</span></code></a></p></td>
  1744. <td><p><a class="reference internal" href="#pathlib.Path.samefile" title="pathlib.Path.samefile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.samefile()</span></code></a></p></td>
  1745. </tr>
  1746. <tr class="row-odd"><td><p><a class="reference internal" href="os.path.html#os.path.splitext" title="os.path.splitext"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.splitext()</span></code></a></p></td>
  1747. <td><p><a class="reference internal" href="#pathlib.PurePath.stem" title="pathlib.PurePath.stem"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PurePath.stem</span></code></a> and
  1748. <a class="reference internal" href="#pathlib.PurePath.suffix" title="pathlib.PurePath.suffix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PurePath.suffix</span></code></a></p></td>
  1749. </tr>
  1750. </tbody>
  1751. </table>
  1752. <p class="rubric">Footnotes</p>
  1753. <aside class="footnote-list brackets">
  1754. <aside class="footnote brackets" id="id5" role="doc-footnote">
  1755. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id3">1</a><span class="fn-bracket">]</span></span>
  1756. <p><a class="reference internal" href="os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.abspath()</span></code></a> normalizes the resulting path, which may change its meaning in the presence of symlinks, while <a class="reference internal" href="#pathlib.Path.absolute" title="pathlib.Path.absolute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.absolute()</span></code></a> does not.</p>
  1757. </aside>
  1758. <aside class="footnote brackets" id="id6" role="doc-footnote">
  1759. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id4">2</a><span class="fn-bracket">]</span></span>
  1760. <p><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">PurePath.relative_to()</span></code></a> requires <code class="docutils literal notranslate"><span class="pre">self</span></code> to be the subpath of the argument, but <a class="reference internal" href="os.path.html#os.path.relpath" title="os.path.relpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.relpath()</span></code></a> does not.</p>
  1761. </aside>
  1762. </aside>
  1763. </section>
  1764. </section>
  1765. <div class="clearer"></div>
  1766. </div>
  1767. </div>
  1768. </div>
  1769. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1770. <div class="sphinxsidebarwrapper">
  1771. <div>
  1772. <h3><a href="../contents.html">Table of Contents</a></h3>
  1773. <ul>
  1774. <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>
  1775. <li><a class="reference internal" href="#basic-use">Basic use</a></li>
  1776. <li><a class="reference internal" href="#pure-paths">Pure paths</a><ul>
  1777. <li><a class="reference internal" href="#general-properties">General properties</a></li>
  1778. <li><a class="reference internal" href="#operators">Operators</a></li>
  1779. <li><a class="reference internal" href="#accessing-individual-parts">Accessing individual parts</a></li>
  1780. <li><a class="reference internal" href="#methods-and-properties">Methods and properties</a></li>
  1781. </ul>
  1782. </li>
  1783. <li><a class="reference internal" href="#concrete-paths">Concrete paths</a><ul>
  1784. <li><a class="reference internal" href="#methods">Methods</a></li>
  1785. </ul>
  1786. </li>
  1787. <li><a class="reference internal" href="#correspondence-to-tools-in-the-os-module">Correspondence to tools in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code> module</a></li>
  1788. </ul>
  1789. </li>
  1790. </ul>
  1791. </div>
  1792. <div>
  1793. <h4>Previous topic</h4>
  1794. <p class="topless"><a href="filesys.html"
  1795. title="previous chapter">File and Directory Access</a></p>
  1796. </div>
  1797. <div>
  1798. <h4>Next topic</h4>
  1799. <p class="topless"><a href="os.path.html"
  1800. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code> — Common pathname manipulations</a></p>
  1801. </div>
  1802. <div role="note" aria-label="source link">
  1803. <h3>This Page</h3>
  1804. <ul class="this-page-menu">
  1805. <li><a href="../bugs.html">Report a Bug</a></li>
  1806. <li>
  1807. <a href="https://github.com/python/cpython/blob/main/Doc/library/pathlib.rst"
  1808. rel="nofollow">Show Source
  1809. </a>
  1810. </li>
  1811. </ul>
  1812. </div>
  1813. </div>
  1814. <div id="sidebarbutton" title="Collapse sidebar">
  1815. <span>«</span>
  1816. </div>
  1817. </div>
  1818. <div class="clearer"></div>
  1819. </div>
  1820. <div class="related" role="navigation" aria-label="related navigation">
  1821. <h3>Navigation</h3>
  1822. <ul>
  1823. <li class="right" style="margin-right: 10px">
  1824. <a href="../genindex.html" title="General Index"
  1825. >index</a></li>
  1826. <li class="right" >
  1827. <a href="../py-modindex.html" title="Python Module Index"
  1828. >modules</a> |</li>
  1829. <li class="right" >
  1830. <a href="os.path.html" title="os.path — Common pathname manipulations"
  1831. >next</a> |</li>
  1832. <li class="right" >
  1833. <a href="filesys.html" title="File and Directory Access"
  1834. >previous</a> |</li>
  1835. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1836. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1837. <li class="switchers">
  1838. <div class="language_switcher_placeholder"></div>
  1839. <div class="version_switcher_placeholder"></div>
  1840. </li>
  1841. <li>
  1842. </li>
  1843. <li id="cpython-language-and-version">
  1844. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1845. </li>
  1846. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1847. <li class="nav-item nav-item-2"><a href="filesys.html" >File and Directory Access</a> &#187;</li>
  1848. <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>
  1849. <li class="right">
  1850. <div class="inline-search" role="search">
  1851. <form class="inline-search" action="../search.html" method="get">
  1852. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1853. <input type="submit" value="Go" />
  1854. </form>
  1855. </div>
  1856. |
  1857. </li>
  1858. <li class="right">
  1859. <label class="theme-selector-label">
  1860. Theme
  1861. <select class="theme-selector" oninput="activateTheme(this.value)">
  1862. <option value="auto" selected>Auto</option>
  1863. <option value="light">Light</option>
  1864. <option value="dark">Dark</option>
  1865. </select>
  1866. </label> |</li>
  1867. </ul>
  1868. </div>
  1869. <div class="footer">
  1870. &copy;
  1871. <a href="../copyright.html">
  1872. Copyright
  1873. </a>
  1874. 2001-2024, Python Software Foundation.
  1875. <br />
  1876. This page is licensed under the Python Software Foundation License Version 2.
  1877. <br />
  1878. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1879. <br />
  1880. See <a href="/license.html">History and License</a> for more information.<br />
  1881. <br />
  1882. The Python Software Foundation is a non-profit corporation.
  1883. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1884. <br />
  1885. <br />
  1886. Last updated on Apr 09, 2024 (13:47 UTC).
  1887. <a href="/bugs.html">Found a bug</a>?
  1888. <br />
  1889. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1890. </div>
  1891. </body>
  1892. </html>
上海开阖软件有限公司 沪ICP备12045867号-1