diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Fpathlib.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Fpathlib.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Fpathlib.html | 690 |
1 files changed, 690 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fpathlib.html b/devdocs/python~3.12/library%2Fpathlib.html new file mode 100644 index 00000000..7ec3802a --- /dev/null +++ b/devdocs/python~3.12/library%2Fpathlib.html @@ -0,0 +1,690 @@ + <span id="pathlib-object-oriented-filesystem-paths"></span><h1>pathlib — Object-oriented filesystem paths</h1> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> <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> <p>This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between <a class="reference internal" href="#pure-paths"><span class="std std-ref">pure paths</span></a>, which provide purely computational operations without I/O, and <a class="reference internal" href="#concrete-paths"><span class="std std-ref">concrete paths</span></a>, which inherit from pure paths but also provide I/O operations.</p> <img alt="../_images/pathlib-inheritance.png" class="invert-in-dark-mode align-center" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhoAAAE/CAMAAAA37poMAAAA21BMVEVHcEwAAAAAAAAAAAADAwMREREcHBwAAAAZGRkJCQkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAiIiInJyf///8AAAAgICBfX1+goKAFBQV/f3/f399AQED6+vrt7e0NDQ2/v78WFhbm5uYxMTHz8/NmZmY6OjpVVVXFxcW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19JywaXVlUuBSj/+oCajCGIamq1D0wgx9qSP9Sd1G55LjudTOyod9FsooOZfilklsfUtdyptmNldLomJyox9NpGjU9nend3NSMRo+3aWKG6YiBfg7f9ERbP3AX7jwOP+nJ1BtQ73eN24F3bZpBNbxJNA3X5D7C2OTdfhCVT1nfuddFwwVzNzJqXCxcWVgjozFwwzK2Re9GTyKpkdE1WcE44JeFv/pHLvQnZQZ6nshqGA316PH3yuENjqHgGAoOr4EGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIAGaIDG36WR/TfOSceTRrwLbU0KEUEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEHWO/8D3b5/1wp9XEIAAAAASUVORK5CYII="> <p>If you’ve never used this module before or just aren’t sure which class is right for your task, <a class="reference internal" href="#pathlib.Path" title="pathlib.Path"><code>Path</code></a> is most likely what you need. It instantiates 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> <p>Pure paths are useful in some special cases; for example:</p> <ol class="arabic simple"> <li>If you want to manipulate Windows paths on a Unix machine (or vice versa). You cannot instantiate a <a class="reference internal" href="#pathlib.WindowsPath" title="pathlib.WindowsPath"><code>WindowsPath</code></a> when running on Unix, but you can instantiate <a class="reference internal" href="#pathlib.PureWindowsPath" title="pathlib.PureWindowsPath"><code>PureWindowsPath</code></a>.</li> <li>You want to make sure that your code only manipulates paths without actually accessing the OS. In this case, instantiating one of the pure classes may be useful since those simply don’t have any OS-accessing operations.</li> </ol> <div class="admonition seealso"> <p class="admonition-title">See also</p> <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> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>For low-level path manipulation on strings, you can also use the <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a> module.</p> </div> <section id="basic-use"> <h2>Basic use</h2> <p>Importing the main class:</p> <pre data-language="python">>>> from pathlib import Path +</pre> <p>Listing subdirectories:</p> <pre data-language="python">>>> p = Path('.') +>>> [x for x in p.iterdir() if x.is_dir()] +[PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'), + PosixPath('__pycache__'), PosixPath('build')] +</pre> <p>Listing Python source files in this directory tree:</p> <pre data-language="python">>>> list(p.glob('**/*.py')) +[PosixPath('test_pathlib.py'), PosixPath('setup.py'), + PosixPath('pathlib.py'), PosixPath('docs/conf.py'), + PosixPath('build/lib/pathlib.py')] +</pre> <p>Navigating inside a directory tree:</p> <pre data-language="python">>>> p = Path('/etc') +>>> q = p / 'init.d' / 'reboot' +>>> q +PosixPath('/etc/init.d/reboot') +>>> q.resolve() +PosixPath('/etc/rc.d/init.d/halt') +</pre> <p>Querying path properties:</p> <pre data-language="python">>>> q.exists() +True +>>> q.is_dir() +False +</pre> <p>Opening a file:</p> <pre data-language="python">>>> with q.open() as f: f.readline() +... +'#!/bin/bash\n' +</pre> </section> <section id="pure-paths"> <span id="id1"></span><h2>Pure paths</h2> <p>Pure path objects provide path-handling operations which don’t actually access a filesystem. There are three ways to access these classes, which we also call <em>flavours</em>:</p> <dl class="py class"> <dt class="sig sig-object py" id="pathlib.PurePath"> +<code>class pathlib.PurePath(*pathsegments)</code> </dt> <dd> +<p>A generic class that represents the system’s path flavour (instantiating it creates either a <a class="reference internal" href="#pathlib.PurePosixPath" title="pathlib.PurePosixPath"><code>PurePosixPath</code></a> or a <a class="reference internal" href="#pathlib.PureWindowsPath" title="pathlib.PureWindowsPath"><code>PureWindowsPath</code></a>):</p> <pre data-language="python">>>> PurePath('setup.py') # Running on a Unix machine +PurePosixPath('setup.py') +</pre> <p>Each element of <em>pathsegments</em> can be either a string representing a path segment, or an object implementing the <a class="reference internal" href="os#os.PathLike" title="os.PathLike"><code>os.PathLike</code></a> interface where the <a class="reference internal" href="os#os.PathLike.__fspath__" title="os.PathLike.__fspath__"><code>__fspath__()</code></a> method returns a string, such as another path object:</p> <pre data-language="python">>>> PurePath('foo', 'some/path', 'bar') +PurePosixPath('foo/some/path/bar') +>>> PurePath(Path('foo'), Path('bar')) +PurePosixPath('foo/bar') +</pre> <p>When <em>pathsegments</em> is empty, the current directory is assumed:</p> <pre data-language="python">>>> PurePath() +PurePosixPath('.') +</pre> <p>If a segment is an absolute path, all previous segments are ignored (like <a class="reference internal" href="os.path#os.path.join" title="os.path.join"><code>os.path.join()</code></a>):</p> <pre data-language="python">>>> PurePath('/etc', '/usr', 'lib64') +PurePosixPath('/usr/lib64') +>>> PureWindowsPath('c:/Windows', 'd:bar') +PureWindowsPath('d:bar') +</pre> <p>On Windows, the drive is not reset when a rooted relative path segment (e.g., <code>r'\foo'</code>) is encountered:</p> <pre data-language="python">>>> PureWindowsPath('c:/Windows', '/Program Files') +PureWindowsPath('c:/Program Files') +</pre> <p>Spurious slashes and single dots are collapsed, but double dots (<code>'..'</code>) and leading double slashes (<code>'//'</code>) are not, since this would change the meaning of a path for various reasons (e.g. symbolic links, UNC paths):</p> <pre data-language="python">>>> PurePath('foo//bar') +PurePosixPath('foo/bar') +>>> PurePath('//foo/bar') +PurePosixPath('//foo/bar') +>>> PurePath('foo/./bar') +PurePosixPath('foo/bar') +>>> PurePath('foo/../bar') +PurePosixPath('foo/../bar') +</pre> <p>(a naïve approach would make <code>PurePosixPath('foo/../bar')</code> equivalent to <code>PurePosixPath('bar')</code>, which is wrong if <code>foo</code> is a symbolic link to another directory)</p> <p>Pure path objects implement the <a class="reference internal" href="os#os.PathLike" title="os.PathLike"><code>os.PathLike</code></a> interface, allowing them to be used anywhere the interface is accepted.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Added support for the <a class="reference internal" href="os#os.PathLike" title="os.PathLike"><code>os.PathLike</code></a> interface.</p> </div> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="pathlib.PurePosixPath"> +<code>class pathlib.PurePosixPath(*pathsegments)</code> </dt> <dd> +<p>A subclass of <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a>, this path flavour represents non-Windows filesystem paths:</p> <pre data-language="python">>>> PurePosixPath('/etc') +PurePosixPath('/etc') +</pre> <p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a>.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="pathlib.PureWindowsPath"> +<code>class pathlib.PureWindowsPath(*pathsegments)</code> </dt> <dd> +<p>A subclass of <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a>, this path flavour represents Windows filesystem paths, including <a class="reference external" href="https://en.wikipedia.org/wiki/Path_(computing)#UNC">UNC paths</a>:</p> <pre data-language="python">>>> PureWindowsPath('c:/Program Files/') +PureWindowsPath('c:/Program Files') +>>> PureWindowsPath('//server/share/file') +PureWindowsPath('//server/share/file') +</pre> <p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a>.</p> </dd> +</dl> <p>Regardless of the system you’re running on, you can instantiate all of these classes, since they don’t provide any operation that does system calls.</p> <section id="general-properties"> <h3>General properties</h3> <p>Paths are immutable and <a class="reference internal" href="../glossary#term-hashable"><span class="xref std std-term">hashable</span></a>. Paths of a same flavour are comparable and orderable. These properties respect the flavour’s case-folding semantics:</p> <pre data-language="python">>>> PurePosixPath('foo') == PurePosixPath('FOO') +False +>>> PureWindowsPath('foo') == PureWindowsPath('FOO') +True +>>> PureWindowsPath('FOO') in { PureWindowsPath('foo') } +True +>>> PureWindowsPath('C:') < PureWindowsPath('d:') +True +</pre> <p>Paths of a different flavour compare unequal and cannot be ordered:</p> <pre data-language="python">>>> PureWindowsPath('foo') == PurePosixPath('foo') +False +>>> PureWindowsPath('foo') < PurePosixPath('foo') +Traceback (most recent call last): + File "<stdin>", line 1, in <module> +TypeError: '<' not supported between instances of 'PureWindowsPath' and 'PurePosixPath' +</pre> </section> <section id="operators"> <h3>Operators</h3> <p>The slash operator helps create child paths, like <a class="reference internal" href="os.path#os.path.join" title="os.path.join"><code>os.path.join()</code></a>. If the argument is an absolute path, the previous path is ignored. On Windows, the drive is not reset when the argument is a rooted relative path (e.g., <code>r'\foo'</code>):</p> <pre data-language="python">>>> p = PurePath('/etc') +>>> p +PurePosixPath('/etc') +>>> p / 'init.d' / 'apache2' +PurePosixPath('/etc/init.d/apache2') +>>> q = PurePath('bin') +>>> '/usr' / q +PurePosixPath('/usr/bin') +>>> p / '/an_absolute_path' +PurePosixPath('/an_absolute_path') +>>> PureWindowsPath('c:/Windows', '/Program Files') +PureWindowsPath('c:/Program Files') +</pre> <p>A path object can be used anywhere an object implementing <a class="reference internal" href="os#os.PathLike" title="os.PathLike"><code>os.PathLike</code></a> is accepted:</p> <pre data-language="python">>>> import os +>>> p = PurePath('/etc') +>>> os.fspath(p) +'/etc' +</pre> <p>The string representation of a path is the raw filesystem path itself (in native form, e.g. with backslashes under Windows), which you can pass to any function taking a file path as a string:</p> <pre data-language="python">>>> p = PurePath('/etc') +>>> str(p) +'/etc' +>>> p = PureWindowsPath('c:/Program Files') +>>> str(p) +'c:\\Program Files' +</pre> <p>Similarly, calling <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> on a path gives the raw filesystem path as a bytes object, as encoded by <a class="reference internal" href="os#os.fsencode" title="os.fsencode"><code>os.fsencode()</code></a>:</p> <pre data-language="python">>>> bytes(p) +b'/etc' +</pre> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Calling <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> is only recommended under Unix. Under Windows, the unicode form is the canonical representation of filesystem paths.</p> </div> </section> <section id="accessing-individual-parts"> <h3>Accessing individual parts</h3> <p>To access the individual “parts” (components) of a path, use the following property:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.parts"> +<code>PurePath.parts</code> </dt> <dd> +<p>A tuple giving access to the path’s various components:</p> <pre data-language="python">>>> p = PurePath('/usr/bin/python3') +>>> p.parts +('/', 'usr', 'bin', 'python3') + +>>> p = PureWindowsPath('c:/Program Files/PSF') +>>> p.parts +('c:\\', 'Program Files', 'PSF') +</pre> <p>(note how the drive and local root are regrouped in a single part)</p> </dd> +</dl> </section> <section id="methods-and-properties"> <h3>Methods and properties</h3> <p>Pure paths provide the following methods and properties:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.drive"> +<code>PurePath.drive</code> </dt> <dd> +<p>A string representing the drive letter or name, if any:</p> <pre data-language="python">>>> PureWindowsPath('c:/Program Files/').drive +'c:' +>>> PureWindowsPath('/Program Files/').drive +'' +>>> PurePosixPath('/etc').drive +'' +</pre> <p>UNC shares are also considered drives:</p> <pre data-language="python">>>> PureWindowsPath('//host/share/foo.txt').drive +'\\\\host\\share' +</pre> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.root"> +<code>PurePath.root</code> </dt> <dd> +<p>A string representing the (local or global) root, if any:</p> <pre data-language="python">>>> PureWindowsPath('c:/Program Files/').root +'\\' +>>> PureWindowsPath('c:Program Files/').root +'' +>>> PurePosixPath('/etc').root +'/' +</pre> <p>UNC shares always have a root:</p> <pre data-language="python">>>> PureWindowsPath('//host/share').root +'\\' +</pre> <p>If the path starts with more than two successive slashes, <a class="reference internal" href="#pathlib.PurePosixPath" title="pathlib.PurePosixPath"><code>PurePosixPath</code></a> collapses them:</p> <pre data-language="python">>>> PurePosixPath('//etc').root +'//' +>>> PurePosixPath('///etc').root +'/' +>>> PurePosixPath('////etc').root +'/' +</pre> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This behavior conforms to <em>The Open Group Base Specifications Issue 6</em>, 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> <p><em>“A pathname that begins with two successive slashes may be interpreted in an implementation-defined manner, although more than two leading slashes shall be treated as a single slash.”</em></p> </div> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.anchor"> +<code>PurePath.anchor</code> </dt> <dd> +<p>The concatenation of the drive and root:</p> <pre data-language="python">>>> PureWindowsPath('c:/Program Files/').anchor +'c:\\' +>>> PureWindowsPath('c:Program Files/').anchor +'c:' +>>> PurePosixPath('/etc').anchor +'/' +>>> PureWindowsPath('//host/share').anchor +'\\\\host\\share\\' +</pre> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.parents"> +<code>PurePath.parents</code> </dt> <dd> +<p>An immutable sequence providing access to the logical ancestors of the path:</p> <pre data-language="python">>>> p = PureWindowsPath('c:/foo/bar/setup.py') +>>> p.parents[0] +PureWindowsPath('c:/foo/bar') +>>> p.parents[1] +PureWindowsPath('c:/foo') +>>> p.parents[2] +PureWindowsPath('c:/') +</pre> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>The parents sequence now supports <a class="reference internal" href="../glossary#term-slice"><span class="xref std std-term">slices</span></a> and negative index values.</p> </div> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.parent"> +<code>PurePath.parent</code> </dt> <dd> +<p>The logical parent of the path:</p> <pre data-language="python">>>> p = PurePosixPath('/a/b/c/d') +>>> p.parent +PurePosixPath('/a/b/c') +</pre> <p>You cannot go past an anchor, or empty path:</p> <pre data-language="python">>>> p = PurePosixPath('/') +>>> p.parent +PurePosixPath('/') +>>> p = PurePosixPath('.') +>>> p.parent +PurePosixPath('.') +</pre> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This is a purely lexical operation, hence the following behaviour:</p> <pre data-language="python">>>> p = PurePosixPath('foo/..') +>>> p.parent +PurePosixPath('foo') +</pre> <p>If you want to walk an arbitrary filesystem path upwards, it is recommended to first call <a class="reference internal" href="#pathlib.Path.resolve" title="pathlib.Path.resolve"><code>Path.resolve()</code></a> so as to resolve symlinks and eliminate <code>".."</code> components.</p> </div> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.name"> +<code>PurePath.name</code> </dt> <dd> +<p>A string representing the final path component, excluding the drive and root, if any:</p> <pre data-language="python">>>> PurePosixPath('my/library/setup.py').name +'setup.py' +</pre> <p>UNC drive names are not considered:</p> <pre data-language="python">>>> PureWindowsPath('//some/share/setup.py').name +'setup.py' +>>> PureWindowsPath('//some/share').name +'' +</pre> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.suffix"> +<code>PurePath.suffix</code> </dt> <dd> +<p>The file extension of the final component, if any:</p> <pre data-language="python">>>> PurePosixPath('my/library/setup.py').suffix +'.py' +>>> PurePosixPath('my/library.tar.gz').suffix +'.gz' +>>> PurePosixPath('my/library').suffix +'' +</pre> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.suffixes"> +<code>PurePath.suffixes</code> </dt> <dd> +<p>A list of the path’s file extensions:</p> <pre data-language="python">>>> PurePosixPath('my/library.tar.gar').suffixes +['.tar', '.gar'] +>>> PurePosixPath('my/library.tar.gz').suffixes +['.tar', '.gz'] +>>> PurePosixPath('my/library').suffixes +[] +</pre> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="pathlib.PurePath.stem"> +<code>PurePath.stem</code> </dt> <dd> +<p>The final path component, without its suffix:</p> <pre data-language="python">>>> PurePosixPath('my/library.tar.gz').stem +'library.tar' +>>> PurePosixPath('my/library.tar').stem +'library' +>>> PurePosixPath('my/library').stem +'library' +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.as_posix"> +<code>PurePath.as_posix()</code> </dt> <dd> +<p>Return a string representation of the path with forward slashes (<code>/</code>):</p> <pre data-language="python">>>> p = PureWindowsPath('c:\\windows') +>>> str(p) +'c:\\windows' +>>> p.as_posix() +'c:/windows' +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.as_uri"> +<code>PurePath.as_uri()</code> </dt> <dd> +<p>Represent the path as a <code>file</code> URI. <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised if the path isn’t absolute.</p> <pre data-language="python">>>> p = PurePosixPath('/etc/passwd') +>>> p.as_uri() +'file:///etc/passwd' +>>> p = PureWindowsPath('c:/Windows') +>>> p.as_uri() +'file:///c:/Windows' +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.is_absolute"> +<code>PurePath.is_absolute()</code> </dt> <dd> +<p>Return whether the path is absolute or not. A path is considered absolute if it has both a root and (if the flavour allows) a drive:</p> <pre data-language="python">>>> PurePosixPath('/a/b').is_absolute() +True +>>> PurePosixPath('a/b').is_absolute() +False + +>>> PureWindowsPath('c:/a/b').is_absolute() +True +>>> PureWindowsPath('/a/b').is_absolute() +False +>>> PureWindowsPath('c:').is_absolute() +False +>>> PureWindowsPath('//some/share').is_absolute() +True +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.is_relative_to"> +<code>PurePath.is_relative_to(other)</code> </dt> <dd> +<p>Return whether or not this path is relative to the <em>other</em> path.</p> <pre data-language="python">>>> p = PurePath('/etc/passwd') +>>> p.is_relative_to('/etc') +True +>>> p.is_relative_to('/usr') +False +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> <div class="deprecated-removed"> <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 with <em>other</em>.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.is_reserved"> +<code>PurePath.is_reserved()</code> </dt> <dd> +<p>With <a class="reference internal" href="#pathlib.PureWindowsPath" title="pathlib.PureWindowsPath"><code>PureWindowsPath</code></a>, return <code>True</code> if the path is considered reserved under Windows, <code>False</code> otherwise. With <a class="reference internal" href="#pathlib.PurePosixPath" title="pathlib.PurePosixPath"><code>PurePosixPath</code></a>, <code>False</code> is always returned.</p> <pre data-language="python">>>> PureWindowsPath('nul').is_reserved() +True +>>> PurePosixPath('nul').is_reserved() +False +</pre> <p>File system calls on reserved paths can fail mysteriously or have unintended effects.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.joinpath"> +<code>PurePath.joinpath(*pathsegments)</code> </dt> <dd> +<p>Calling this method is equivalent to combining the path with each of the given <em>pathsegments</em> in turn:</p> <pre data-language="python">>>> PurePosixPath('/etc').joinpath('passwd') +PurePosixPath('/etc/passwd') +>>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd')) +PurePosixPath('/etc/passwd') +>>> PurePosixPath('/etc').joinpath('init.d', 'apache2') +PurePosixPath('/etc/init.d/apache2') +>>> PureWindowsPath('c:').joinpath('/Program Files') +PureWindowsPath('c:/Program Files') +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.match"> +<code>PurePath.match(pattern, *, case_sensitive=None)</code> </dt> <dd> +<p>Match this path against the provided glob-style pattern. Return <code>True</code> if matching is successful, <code>False</code> otherwise.</p> <p>If <em>pattern</em> is relative, the path can be either relative or absolute, and matching is done from the right:</p> <pre data-language="python">>>> PurePath('a/b.py').match('*.py') +True +>>> PurePath('/a/b/c.py').match('b/*.py') +True +>>> PurePath('/a/b/c.py').match('a/*.py') +False +</pre> <p>If <em>pattern</em> is absolute, the path must be absolute, and the whole path must match:</p> <pre data-language="python">>>> PurePath('/a.py').match('/*.py') +True +>>> PurePath('a/b.py').match('/*.py') +False +</pre> <p>The <em>pattern</em> may be another path object; this speeds up matching the same pattern against multiple files:</p> <pre data-language="python">>>> pattern = PurePath('*.py') +>>> PurePath('a/b.py').match(pattern) +True +</pre> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>Accepts an object implementing the <a class="reference internal" href="os#os.PathLike" title="os.PathLike"><code>os.PathLike</code></a> interface.</p> </div> <p>As with other methods, case-sensitivity follows platform defaults:</p> <pre data-language="python">>>> PurePosixPath('b.py').match('*.PY') +False +>>> PureWindowsPath('b.py').match('*.PY') +True +</pre> <p>Set <em>case_sensitive</em> to <code>True</code> or <code>False</code> to override this behaviour.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>case_sensitive</em> parameter was added.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.relative_to"> +<code>PurePath.relative_to(other, walk_up=False)</code> </dt> <dd> +<p>Compute a version of this path relative to the path represented by <em>other</em>. If it’s impossible, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised:</p> <pre data-language="python">>>> p = PurePosixPath('/etc/passwd') +>>> p.relative_to('/') +PurePosixPath('etc/passwd') +>>> p.relative_to('/etc') +PurePosixPath('passwd') +>>> p.relative_to('/usr') +Traceback (most recent call last): + File "<stdin>", line 1, in <module> + File "pathlib.py", line 941, in relative_to + raise ValueError(error_message.format(str(self), str(formatted))) +ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other is absolute. +</pre> <p>When <em>walk_up</em> is False (the default), the path must start with <em>other</em>. When the argument is True, <code>..</code> entries may be added to form the relative path. In all other cases, such as the paths referencing different drives, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised.:</p> <pre data-language="python">>>> p.relative_to('/usr', walk_up=True) +PurePosixPath('../etc/passwd') +>>> p.relative_to('foo', walk_up=True) +Traceback (most recent call last): + File "<stdin>", line 1, in <module> + File "pathlib.py", line 941, in relative_to + raise ValueError(error_message.format(str(self), str(formatted))) +ValueError: '/etc/passwd' is not on the same drive as 'foo' OR one path is relative and the other is absolute. +</pre> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>This function is part of <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a> and works with strings. It does not check or access the underlying file structure. This can impact the <em>walk_up</em> option as it assumes that no symlinks are present in the path; call <a class="reference internal" href="#pathlib.Path.resolve" title="pathlib.Path.resolve"><code>resolve()</code></a> first if necessary to resolve symlinks.</p> </div> <div class="versionchanged"> <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>walk_up=False</code>).</p> </div> <div class="deprecated-removed"> <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, they are joined with <em>other</em>.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.with_name"> +<code>PurePath.with_name(name)</code> </dt> <dd> +<p>Return a new path with the <a class="reference internal" href="#pathlib.PurePath.name" title="pathlib.PurePath.name"><code>name</code></a> changed. If the original path doesn’t have a name, ValueError is raised:</p> <pre data-language="python">>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz') +>>> p.with_name('setup.py') +PureWindowsPath('c:/Downloads/setup.py') +>>> p = PureWindowsPath('c:/') +>>> p.with_name('setup.py') +Traceback (most recent call last): + File "<stdin>", line 1, in <module> + File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_name + raise ValueError("%r has an empty name" % (self,)) +ValueError: PureWindowsPath('c:/') has an empty name +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.with_stem"> +<code>PurePath.with_stem(stem)</code> </dt> <dd> +<p>Return a new path with the <a class="reference internal" href="#pathlib.PurePath.stem" title="pathlib.PurePath.stem"><code>stem</code></a> changed. If the original path doesn’t have a name, ValueError is raised:</p> <pre data-language="python">>>> p = PureWindowsPath('c:/Downloads/draft.txt') +>>> p.with_stem('final') +PureWindowsPath('c:/Downloads/final.txt') +>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz') +>>> p.with_stem('lib') +PureWindowsPath('c:/Downloads/lib.gz') +>>> p = PureWindowsPath('c:/') +>>> p.with_stem('') +Traceback (most recent call last): + File "<stdin>", line 1, in <module> + File "/home/antoine/cpython/default/Lib/pathlib.py", line 861, in with_stem + return self.with_name(stem + self.suffix) + File "/home/antoine/cpython/default/Lib/pathlib.py", line 851, in with_name + raise ValueError("%r has an empty name" % (self,)) +ValueError: PureWindowsPath('c:/') has an empty name +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.with_suffix"> +<code>PurePath.with_suffix(suffix)</code> </dt> <dd> +<p>Return a new path with the <a class="reference internal" href="#pathlib.PurePath.suffix" title="pathlib.PurePath.suffix"><code>suffix</code></a> changed. If the original path doesn’t have a suffix, the new <em>suffix</em> is appended instead. If the <em>suffix</em> is an empty string, the original suffix is removed:</p> <pre data-language="python">>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz') +>>> p.with_suffix('.bz2') +PureWindowsPath('c:/Downloads/pathlib.tar.bz2') +>>> p = PureWindowsPath('README') +>>> p.with_suffix('.txt') +PureWindowsPath('README.txt') +>>> p = PureWindowsPath('README.txt') +>>> p.with_suffix('') +PureWindowsPath('README') +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.PurePath.with_segments"> +<code>PurePath.with_segments(*pathsegments)</code> </dt> <dd> +<p>Create a new path object of the same type by combining the given <em>pathsegments</em>. This method is called whenever a derivative path is created, such as from <a class="reference internal" href="#pathlib.PurePath.parent" title="pathlib.PurePath.parent"><code>parent</code></a> and <a class="reference internal" href="#pathlib.PurePath.relative_to" title="pathlib.PurePath.relative_to"><code>relative_to()</code></a>. Subclasses may override this method to pass information to derivative paths, for example:</p> <pre data-language="python">from pathlib import PurePosixPath + +class MyPath(PurePosixPath): + def __init__(self, *pathsegments, session_id): + super().__init__(*pathsegments) + self.session_id = session_id + + def with_segments(self, *pathsegments): + return type(self)(*pathsegments, session_id=self.session_id) + +etc = MyPath('/etc', session_id=42) +hosts = etc / 'hosts' +print(hosts.session_id) # 42 +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd> +</dl> </section> </section> <section id="concrete-paths"> <span id="id2"></span><h2>Concrete paths</h2> <p>Concrete paths are subclasses of the pure path classes. In addition to operations provided by the latter, they also provide methods to do system calls on path objects. There are three ways to instantiate concrete paths:</p> <dl class="py class"> <dt class="sig sig-object py" id="pathlib.Path"> +<code>class pathlib.Path(*pathsegments)</code> </dt> <dd> +<p>A subclass of <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a>, this class represents concrete paths of the system’s path flavour (instantiating it creates either a <a class="reference internal" href="#pathlib.PosixPath" title="pathlib.PosixPath"><code>PosixPath</code></a> or a <a class="reference internal" href="#pathlib.WindowsPath" title="pathlib.WindowsPath"><code>WindowsPath</code></a>):</p> <pre data-language="python">>>> Path('setup.py') +PosixPath('setup.py') +</pre> <p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a>.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="pathlib.PosixPath"> +<code>class pathlib.PosixPath(*pathsegments)</code> </dt> <dd> +<p>A subclass of <a class="reference internal" href="#pathlib.Path" title="pathlib.Path"><code>Path</code></a> and <a class="reference internal" href="#pathlib.PurePosixPath" title="pathlib.PurePosixPath"><code>PurePosixPath</code></a>, this class represents concrete non-Windows filesystem paths:</p> <pre data-language="python">>>> PosixPath('/etc') +PosixPath('/etc') +</pre> <p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a>.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="pathlib.WindowsPath"> +<code>class pathlib.WindowsPath(*pathsegments)</code> </dt> <dd> +<p>A subclass of <a class="reference internal" href="#pathlib.Path" title="pathlib.Path"><code>Path</code></a> and <a class="reference internal" href="#pathlib.PureWindowsPath" title="pathlib.PureWindowsPath"><code>PureWindowsPath</code></a>, this class represents concrete Windows filesystem paths:</p> <pre data-language="python">>>> WindowsPath('c:/Program Files/') +WindowsPath('c:/Program Files') +</pre> <p><em>pathsegments</em> is specified similarly to <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a>.</p> </dd> +</dl> <p>You can only instantiate the class flavour that corresponds to your system (allowing system calls on non-compatible path flavours could lead to bugs or failures in your application):</p> <pre data-language="python">>>> import os +>>> os.name +'posix' +>>> Path('setup.py') +PosixPath('setup.py') +>>> PosixPath('setup.py') +PosixPath('setup.py') +>>> WindowsPath('setup.py') +Traceback (most recent call last): + File "<stdin>", line 1, in <module> + File "pathlib.py", line 798, in __new__ + % (cls.__name__,)) +NotImplementedError: cannot instantiate 'WindowsPath' on your system +</pre> <section id="methods"> <h3>Methods</h3> <p>Concrete paths provide the following methods in addition to pure paths methods. Many of these methods can raise an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> if a system call fails (for example because the path doesn’t exist).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span><a class="reference internal" href="#pathlib.Path.exists" title="pathlib.Path.exists"><code>exists()</code></a>, <a class="reference internal" href="#pathlib.Path.is_dir" title="pathlib.Path.is_dir"><code>is_dir()</code></a>, <a class="reference internal" href="#pathlib.Path.is_file" title="pathlib.Path.is_file"><code>is_file()</code></a>, <a class="reference internal" href="#pathlib.Path.is_mount" title="pathlib.Path.is_mount"><code>is_mount()</code></a>, <a class="reference internal" href="#pathlib.Path.is_symlink" title="pathlib.Path.is_symlink"><code>is_symlink()</code></a>, <a class="reference internal" href="#pathlib.Path.is_block_device" title="pathlib.Path.is_block_device"><code>is_block_device()</code></a>, <a class="reference internal" href="#pathlib.Path.is_char_device" title="pathlib.Path.is_char_device"><code>is_char_device()</code></a>, <a class="reference internal" href="#pathlib.Path.is_fifo" title="pathlib.Path.is_fifo"><code>is_fifo()</code></a>, <a class="reference internal" href="#pathlib.Path.is_socket" title="pathlib.Path.is_socket"><code>is_socket()</code></a> now return <code>False</code> instead of raising an exception for paths that contain characters unrepresentable at the OS level.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.cwd"> +<code>classmethod Path.cwd()</code> </dt> <dd> +<p>Return a new path object representing the current directory (as returned by <a class="reference internal" href="os#os.getcwd" title="os.getcwd"><code>os.getcwd()</code></a>):</p> <pre data-language="python">>>> Path.cwd() +PosixPath('/home/antoine/pathlib') +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.home"> +<code>classmethod Path.home()</code> </dt> <dd> +<p>Return a new path object representing the user’s home directory (as returned by <a class="reference internal" href="os.path#os.path.expanduser" title="os.path.expanduser"><code>os.path.expanduser()</code></a> with <code>~</code> construct). If the home directory can’t be resolved, <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised.</p> <pre data-language="python">>>> Path.home() +PosixPath('/home/antoine') +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.stat"> +<code>Path.stat(*, follow_symlinks=True)</code> </dt> <dd> +<p>Return a <a class="reference internal" href="os#os.stat_result" title="os.stat_result"><code>os.stat_result</code></a> object containing information about this path, like <a class="reference internal" href="os#os.stat" title="os.stat"><code>os.stat()</code></a>. The result is looked up at each call to this method.</p> <p>This method normally follows symlinks; to stat a symlink add the argument <code>follow_symlinks=False</code>, or use <a class="reference internal" href="#pathlib.Path.lstat" title="pathlib.Path.lstat"><code>lstat()</code></a>.</p> <pre data-language="python">>>> p = Path('setup.py') +>>> p.stat().st_size +956 +>>> p.stat().st_mtime +1327883547.852554 +</pre> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>follow_symlinks</em> parameter was added.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.chmod"> +<code>Path.chmod(mode, *, follow_symlinks=True)</code> </dt> <dd> +<p>Change the file mode and permissions, like <a class="reference internal" href="os#os.chmod" title="os.chmod"><code>os.chmod()</code></a>.</p> <p>This method normally follows symlinks. Some Unix flavours support changing permissions on the symlink itself; on these platforms you may add the argument <code>follow_symlinks=False</code>, or use <a class="reference internal" href="#pathlib.Path.lchmod" title="pathlib.Path.lchmod"><code>lchmod()</code></a>.</p> <pre data-language="python">>>> p = Path('setup.py') +>>> p.stat().st_mode +33277 +>>> p.chmod(0o444) +>>> p.stat().st_mode +33060 +</pre> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>follow_symlinks</em> parameter was added.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.exists"> +<code>Path.exists(*, follow_symlinks=True)</code> </dt> <dd> +<p>Return <code>True</code> if the path points to an existing file or directory.</p> <p>This method normally follows symlinks; to check if a symlink exists, add the argument <code>follow_symlinks=False</code>.</p> <pre data-language="python">>>> Path('.').exists() +True +>>> Path('setup.py').exists() +True +>>> Path('/etc').exists() +True +>>> Path('nonexistentfile').exists() +False +</pre> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>follow_symlinks</em> parameter was added.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.expanduser"> +<code>Path.expanduser()</code> </dt> <dd> +<p>Return a new path with expanded <code>~</code> and <code>~user</code> constructs, as returned by <a class="reference internal" href="os.path#os.path.expanduser" title="os.path.expanduser"><code>os.path.expanduser()</code></a>. If a home directory can’t be resolved, <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised.</p> <pre data-language="python">>>> p = PosixPath('~/films/Monty Python') +>>> p.expanduser() +PosixPath('/home/eric/films/Monty Python') +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.glob"> +<code>Path.glob(pattern, *, case_sensitive=None)</code> </dt> <dd> +<p>Glob the given relative <em>pattern</em> in the directory represented by this path, yielding all matching files (of any kind):</p> <pre data-language="python">>>> sorted(Path('.').glob('*.py')) +[PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')] +>>> sorted(Path('.').glob('*/*.py')) +[PosixPath('docs/conf.py')] +</pre> <p>Patterns are the same as for <a class="reference internal" href="fnmatch#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><code>fnmatch</code></a>, with the addition of “<code>**</code>” which means “this directory and all subdirectories, recursively”. In other words, it enables recursive globbing:</p> <pre data-language="python">>>> sorted(Path('.').glob('**/*.py')) +[PosixPath('build/lib/pathlib.py'), + PosixPath('docs/conf.py'), + PosixPath('pathlib.py'), + PosixPath('setup.py'), + PosixPath('test_pathlib.py')] +</pre> <p>By default, or when the <em>case_sensitive</em> keyword-only argument is set to <code>None</code>, this method matches paths using platform-specific casing rules: typically, case-sensitive on POSIX, and case-insensitive on Windows. Set <em>case_sensitive</em> to <code>True</code> or <code>False</code> to override this behaviour.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Using the “<code>**</code>” pattern in large directory trees may consume an inordinate amount of time.</p> </div> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>pathlib.Path.glob</code> with arguments <code>self</code>, <code>pattern</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Return only directories if <em>pattern</em> ends with a pathname components separator (<a class="reference internal" href="os#os.sep" title="os.sep"><code>sep</code></a> or <a class="reference internal" href="os#os.altsep" title="os.altsep"><code>altsep</code></a>).</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>case_sensitive</em> parameter was added.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.group"> +<code>Path.group()</code> </dt> <dd> +<p>Return the name of the group owning the file. <a class="reference internal" href="exceptions#KeyError" title="KeyError"><code>KeyError</code></a> is raised if the file’s gid isn’t found in the system database.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.is_dir"> +<code>Path.is_dir()</code> </dt> <dd> +<p>Return <code>True</code> if the path points to a directory (or a symbolic link pointing to a directory), <code>False</code> if it points to another kind of file.</p> <p><code>False</code> is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.is_file"> +<code>Path.is_file()</code> </dt> <dd> +<p>Return <code>True</code> if the path points to a regular file (or a symbolic link pointing to a regular file), <code>False</code> if it points to another kind of file.</p> <p><code>False</code> is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.is_junction"> +<code>Path.is_junction()</code> </dt> <dd> +<p>Return <code>True</code> if the path points to a junction, and <code>False</code> for any other type of file. Currently only Windows supports junctions.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.is_mount"> +<code>Path.is_mount()</code> </dt> <dd> +<p>Return <code>True</code> if the path is a <em class="dfn">mount point</em>: a point in a file system where a different file system has been mounted. On POSIX, the function checks whether <em>path</em>’s parent, <code>path/..</code>, is on a different device than <em>path</em>, or whether <code>path/..</code> and <em>path</em> point to the same i-node on the same device — this should detect mount points for all Unix and POSIX variants. On Windows, a mount point is considered to be a drive letter root (e.g. <code>c:\</code>), a UNC share (e.g. <code>\\server\share</code>), or a mounted filesystem directory.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>Windows support was added.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.is_symlink"> +<code>Path.is_symlink()</code> </dt> <dd> +<p>Return <code>True</code> if the path points to a symbolic link, <code>False</code> otherwise.</p> <p><code>False</code> is also returned if the path doesn’t exist; other errors (such as permission errors) are propagated.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.is_socket"> +<code>Path.is_socket()</code> </dt> <dd> +<p>Return <code>True</code> if the path points to a Unix socket (or a symbolic link pointing to a Unix socket), <code>False</code> if it points to another kind of file.</p> <p><code>False</code> is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.is_fifo"> +<code>Path.is_fifo()</code> </dt> <dd> +<p>Return <code>True</code> if the path points to a FIFO (or a symbolic link pointing to a FIFO), <code>False</code> if it points to another kind of file.</p> <p><code>False</code> is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.is_block_device"> +<code>Path.is_block_device()</code> </dt> <dd> +<p>Return <code>True</code> if the path points to a block device (or a symbolic link pointing to a block device), <code>False</code> if it points to another kind of file.</p> <p><code>False</code> is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.is_char_device"> +<code>Path.is_char_device()</code> </dt> <dd> +<p>Return <code>True</code> if the path points to a character device (or a symbolic link pointing to a character device), <code>False</code> if it points to another kind of file.</p> <p><code>False</code> is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.iterdir"> +<code>Path.iterdir()</code> </dt> <dd> +<p>When the path points to a directory, yield path objects of the directory contents:</p> <pre data-language="python">>>> p = Path('docs') +>>> for child in p.iterdir(): child +... +PosixPath('docs/conf.py') +PosixPath('docs/_templates') +PosixPath('docs/make.bat') +PosixPath('docs/index.rst') +PosixPath('docs/_build') +PosixPath('docs/_static') +PosixPath('docs/Makefile') +</pre> <p>The children are yielded in arbitrary order, and the special entries <code>'.'</code> and <code>'..'</code> are not included. If a file is removed from or added to the directory after creating the iterator, whether a path object for that file be included is unspecified.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.walk"> +<code>Path.walk(top_down=True, on_error=None, follow_symlinks=False)</code> </dt> <dd> +<p>Generate the file names in a directory tree by walking the tree either top-down or bottom-up.</p> <p>For each directory in the directory tree rooted at <em>self</em> (including <em>self</em> but excluding ‘.’ and ‘..’), the method yields a 3-tuple of <code>(dirpath, dirnames, filenames)</code>.</p> <p><em>dirpath</em> is a <a class="reference internal" href="#pathlib.Path" title="pathlib.Path"><code>Path</code></a> to the directory currently being walked, <em>dirnames</em> is a list of strings for the names of subdirectories in <em>dirpath</em> (excluding <code>'.'</code> and <code>'..'</code>), and <em>filenames</em> is a list of strings for the names of the non-directory files in <em>dirpath</em>. To get a full path (which begins with <em>self</em>) to a file or directory in <em>dirpath</em>, do <code>dirpath / name</code>. Whether or not the lists are sorted is file system-dependent.</p> <p>If the optional argument <em>top_down</em> is true (which is the default), the triple for a directory is generated before the triples for any of its subdirectories (directories are walked top-down). If <em>top_down</em> is false, the triple for a directory is generated after the triples for all of its subdirectories (directories are walked bottom-up). No matter the value of <em>top_down</em>, the list of subdirectories is retrieved before the triples for the directory and its subdirectories are walked.</p> <p>When <em>top_down</em> is true, the caller can modify the <em>dirnames</em> list in-place (for example, using <a class="reference internal" href="../reference/simple_stmts#del"><code>del</code></a> or slice assignment), and <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code>Path.walk()</code></a> will only recurse into the subdirectories whose names remain in <em>dirnames</em>. This can be used to prune the search, or to impose a specific order of visiting, or even to inform <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code>Path.walk()</code></a> about directories the caller creates or renames before it resumes <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code>Path.walk()</code></a> again. Modifying <em>dirnames</em> when <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>Path.walk()</code></a> since the directories in <em>dirnames</em> have already been generated by the time <em>dirnames</em> is yielded to the caller.</p> <p>By default, errors from <a class="reference internal" href="os#os.scandir" title="os.scandir"><code>os.scandir()</code></a> are ignored. If the optional argument <em>on_error</em> is specified, it should be a callable; it will be called with one argument, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> instance. The callable can handle the error to continue the walk or re-raise it to stop the walk. Note that the filename is available as the <code>filename</code> attribute of the exception object.</p> <p>By default, <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code>Path.walk()</code></a> does not follow symbolic links, and instead adds them to the <em>filenames</em> list. Set <em>follow_symlinks</em> to true to resolve symlinks and place them in <em>dirnames</em> and <em>filenames</em> as appropriate for their targets, and consequently visit directories pointed to by symlinks (where supported).</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Be aware that setting <em>follow_symlinks</em> to true can lead to infinite recursion if a link points to a parent directory of itself. <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code>Path.walk()</code></a> does not keep track of the directories it has already visited.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p><a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code>Path.walk()</code></a> assumes the directories it walks are not modified during execution. For example, if a directory from <em>dirnames</em> has been replaced with a symlink and <em>follow_symlinks</em> is false, <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code>Path.walk()</code></a> will still try to descend into it. To prevent such behavior, remove directories from <em>dirnames</em> as appropriate.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Unlike <a class="reference internal" href="os#os.walk" title="os.walk"><code>os.walk()</code></a>, <a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code>Path.walk()</code></a> lists symlinks to directories in <em>filenames</em> if <em>follow_symlinks</em> is false.</p> </div> <p>This example displays the number of bytes used by all files in each directory, while ignoring <code>__pycache__</code> directories:</p> <pre data-language="python">from pathlib import Path +for root, dirs, files in Path("cpython/Lib/concurrent").walk(on_error=print): + print( + root, + "consumes", + sum((root / file).stat().st_size for file in files), + "bytes in", + len(files), + "non-directory files" + ) + if '__pycache__' in dirs: + dirs.remove('__pycache__') +</pre> <p>This next example is a simple implementation of <a class="reference internal" href="shutil#shutil.rmtree" title="shutil.rmtree"><code>shutil.rmtree()</code></a>. Walking the tree bottom-up is essential as <a class="reference internal" href="#pathlib.Path.rmdir" title="pathlib.Path.rmdir"><code>rmdir()</code></a> doesn’t allow deleting a directory before it is empty:</p> <pre data-language="python"># Delete everything reachable from the directory "top". +# CAUTION: This is dangerous! For example, if top == Path('/'), +# it could delete all of your files. +for root, dirs, files in top.walk(top_down=False): + for name in files: + (root / name).unlink() + for name in dirs: + (root / name).rmdir() +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.lchmod"> +<code>Path.lchmod(mode)</code> </dt> <dd> +<p>Like <a class="reference internal" href="#pathlib.Path.chmod" title="pathlib.Path.chmod"><code>Path.chmod()</code></a> but, if the path points to a symbolic link, the symbolic link’s mode is changed rather than its target’s.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.lstat"> +<code>Path.lstat()</code> </dt> <dd> +<p>Like <a class="reference internal" href="#pathlib.Path.stat" title="pathlib.Path.stat"><code>Path.stat()</code></a> but, if the path points to a symbolic link, return the symbolic link’s information rather than its target’s.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.mkdir"> +<code>Path.mkdir(mode=0o777, parents=False, exist_ok=False)</code> </dt> <dd> +<p>Create a new directory at this given path. If <em>mode</em> is given, it is combined with the process’ <code>umask</code> value to determine the file mode and access flags. If the path already exists, <a class="reference internal" href="exceptions#FileExistsError" title="FileExistsError"><code>FileExistsError</code></a> is raised.</p> <p>If <em>parents</em> is true, any missing parents of this path are created as needed; they are created with the default permissions without taking <em>mode</em> into account (mimicking the POSIX <code>mkdir -p</code> command).</p> <p>If <em>parents</em> is false (the default), a missing parent raises <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a>.</p> <p>If <em>exist_ok</em> is false (the default), <a class="reference internal" href="exceptions#FileExistsError" title="FileExistsError"><code>FileExistsError</code></a> is raised if the target directory already exists.</p> <p>If <em>exist_ok</em> is true, <a class="reference internal" href="exceptions#FileExistsError" title="FileExistsError"><code>FileExistsError</code></a> exceptions will be ignored (same behavior as the POSIX <code>mkdir -p</code> command), but only if the last path component is not an existing non-directory file.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>The <em>exist_ok</em> parameter was added.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.open"> +<code>Path.open(mode='r', buffering=- 1, encoding=None, errors=None, newline=None)</code> </dt> <dd> +<p>Open the file pointed to by the path, like the built-in <a class="reference internal" href="functions#open" title="open"><code>open()</code></a> function does:</p> <pre data-language="python">>>> p = Path('setup.py') +>>> with p.open() as f: +... f.readline() +... +'#!/usr/bin/env python3\n' +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.owner"> +<code>Path.owner()</code> </dt> <dd> +<p>Return the name of the user owning the file. <a class="reference internal" href="exceptions#KeyError" title="KeyError"><code>KeyError</code></a> is raised if the file’s uid isn’t found in the system database.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.read_bytes"> +<code>Path.read_bytes()</code> </dt> <dd> +<p>Return the binary contents of the pointed-to file as a bytes object:</p> <pre data-language="python">>>> p = Path('my_binary_file') +>>> p.write_bytes(b'Binary file contents') +20 +>>> p.read_bytes() +b'Binary file contents' +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.read_text"> +<code>Path.read_text(encoding=None, errors=None)</code> </dt> <dd> +<p>Return the decoded contents of the pointed-to file as a string:</p> <pre data-language="python">>>> p = Path('my_text_file') +>>> p.write_text('Text file contents') +18 +>>> p.read_text() +'Text file contents' +</pre> <p>The file is opened and then closed. The optional parameters have the same meaning as in <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.readlink"> +<code>Path.readlink()</code> </dt> <dd> +<p>Return the path to which the symbolic link points (as returned by <a class="reference internal" href="os#os.readlink" title="os.readlink"><code>os.readlink()</code></a>):</p> <pre data-language="python">>>> p = Path('mylink') +>>> p.symlink_to('setup.py') +>>> p.readlink() +PosixPath('setup.py') +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.rename"> +<code>Path.rename(target)</code> </dt> <dd> +<p>Rename this file or directory to the given <em>target</em>, and return a new Path instance pointing to <em>target</em>. On Unix, if <em>target</em> exists and is a file, it will be replaced silently if the user has permission. On Windows, if <em>target</em> exists, <a class="reference internal" href="exceptions#FileExistsError" title="FileExistsError"><code>FileExistsError</code></a> will be raised. <em>target</em> can be either a string or another path object:</p> <pre data-language="python">>>> p = Path('foo') +>>> p.open('w').write('some text') +9 +>>> target = Path('bar') +>>> p.rename(target) +PosixPath('bar') +>>> target.open().read() +'some text' +</pre> <p>The target path may be absolute or relative. Relative paths are interpreted relative to the current working directory, <em>not</em> the directory of the Path object.</p> <p>It is implemented in terms of <a class="reference internal" href="os#os.rename" title="os.rename"><code>os.rename()</code></a> and gives the same guarantees.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Added return value, return the new Path instance.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.replace"> +<code>Path.replace(target)</code> </dt> <dd> +<p>Rename this file or directory to the given <em>target</em>, and return a new Path instance pointing to <em>target</em>. If <em>target</em> points to an existing file or empty directory, it will be unconditionally replaced.</p> <p>The target path may be absolute or relative. Relative paths are interpreted relative to the current working directory, <em>not</em> the directory of the Path object.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Added return value, return the new Path instance.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.absolute"> +<code>Path.absolute()</code> </dt> <dd> +<p>Make the path absolute, without normalization or resolving symlinks. Returns a new path object:</p> <pre data-language="python">>>> p = Path('tests') +>>> p +PosixPath('tests') +>>> p.absolute() +PosixPath('/home/antoine/pathlib/tests') +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.resolve"> +<code>Path.resolve(strict=False)</code> </dt> <dd> +<p>Make the path absolute, resolving any symlinks. A new path object is returned:</p> <pre data-language="python">>>> p = Path() +>>> p +PosixPath('.') +>>> p.resolve() +PosixPath('/home/antoine/pathlib') +</pre> <p>“<code>..</code>” components are also eliminated (this is the only method to do so):</p> <pre data-language="python">>>> p = Path('docs/../setup.py') +>>> p.resolve() +PosixPath('/home/antoine/pathlib/setup.py') +</pre> <p>If the path doesn’t exist and <em>strict</em> is <code>True</code>, <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a> is raised. If <em>strict</em> is <code>False</code>, the path is resolved as far as possible and any remainder is appended without checking whether it exists. If an infinite loop is encountered along the resolution path, <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised.</p> <div class="versionchanged"> <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> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.rglob"> +<code>Path.rglob(pattern, *, case_sensitive=None)</code> </dt> <dd> +<p>Glob the given relative <em>pattern</em> recursively. This is like calling <a class="reference internal" href="#pathlib.Path.glob" title="pathlib.Path.glob"><code>Path.glob()</code></a> with “<code>**/</code>” added in front of the <em>pattern</em>, where <em>patterns</em> are the same as for <a class="reference internal" href="fnmatch#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><code>fnmatch</code></a>:</p> <pre data-language="python">>>> sorted(Path().rglob("*.py")) +[PosixPath('build/lib/pathlib.py'), + PosixPath('docs/conf.py'), + PosixPath('pathlib.py'), + PosixPath('setup.py'), + PosixPath('test_pathlib.py')] +</pre> <p>By default, or when the <em>case_sensitive</em> keyword-only argument is set to <code>None</code>, this method matches paths using platform-specific casing rules: typically, case-sensitive on POSIX, and case-insensitive on Windows. Set <em>case_sensitive</em> to <code>True</code> or <code>False</code> to override this behaviour.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>pathlib.Path.rglob</code> with arguments <code>self</code>, <code>pattern</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Return only directories if <em>pattern</em> ends with a pathname components separator (<a class="reference internal" href="os#os.sep" title="os.sep"><code>sep</code></a> or <a class="reference internal" href="os#os.altsep" title="os.altsep"><code>altsep</code></a>).</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>The <em>case_sensitive</em> parameter was added.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.rmdir"> +<code>Path.rmdir()</code> </dt> <dd> +<p>Remove this directory. The directory must be empty.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.samefile"> +<code>Path.samefile(other_path)</code> </dt> <dd> +<p>Return whether this path points to the same file as <em>other_path</em>, which can be either a Path object, or a string. The semantics are similar to <a class="reference internal" href="os.path#os.path.samefile" title="os.path.samefile"><code>os.path.samefile()</code></a> and <a class="reference internal" href="os.path#os.path.samestat" title="os.path.samestat"><code>os.path.samestat()</code></a>.</p> <p>An <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> can be raised if either file cannot be accessed for some reason.</p> <pre data-language="python">>>> p = Path('spam') +>>> q = Path('eggs') +>>> p.samefile(q) +False +>>> p.samefile('spam') +True +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.symlink_to"> +<code>Path.symlink_to(target, target_is_directory=False)</code> </dt> <dd> +<p>Make this path a symbolic link to <em>target</em>. Under Windows, <em>target_is_directory</em> must be true (default <code>False</code>) if the link’s target is a directory. Under POSIX, <em>target_is_directory</em>’s value is ignored.</p> <pre data-language="python">>>> p = Path('mylink') +>>> p.symlink_to('setup.py') +>>> p.resolve() +PosixPath('/home/antoine/pathlib/setup.py') +>>> p.stat().st_size +956 +>>> p.lstat().st_size +8 +</pre> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The order of arguments (link, target) is the reverse of <a class="reference internal" href="os#os.symlink" title="os.symlink"><code>os.symlink()</code></a>’s.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.hardlink_to"> +<code>Path.hardlink_to(target)</code> </dt> <dd> +<p>Make this path a hard link to the same file as <em>target</em>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The order of arguments (link, target) is the reverse of <a class="reference internal" href="os#os.link" title="os.link"><code>os.link()</code></a>’s.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.touch"> +<code>Path.touch(mode=0o666, exist_ok=True)</code> </dt> <dd> +<p>Create a file at this given path. If <em>mode</em> is given, it is combined with the process’ <code>umask</code> value to determine the file mode and access flags. If the file already exists, the function succeeds if <em>exist_ok</em> is true (and its modification time is updated to the current time), otherwise <a class="reference internal" href="exceptions#FileExistsError" title="FileExistsError"><code>FileExistsError</code></a> is raised.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.unlink"> +<code>Path.unlink(missing_ok=False)</code> </dt> <dd> +<p>Remove this file or symbolic link. If the path points to a directory, use <a class="reference internal" href="#pathlib.Path.rmdir" title="pathlib.Path.rmdir"><code>Path.rmdir()</code></a> instead.</p> <p>If <em>missing_ok</em> is false (the default), <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a> is raised if the path does not exist.</p> <p>If <em>missing_ok</em> is true, <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a> exceptions will be ignored (same behavior as the POSIX <code>rm -f</code> command).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>The <em>missing_ok</em> parameter was added.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.write_bytes"> +<code>Path.write_bytes(data)</code> </dt> <dd> +<p>Open the file pointed to in bytes mode, write <em>data</em> to it, and close the file:</p> <pre data-language="python">>>> p = Path('my_binary_file') +>>> p.write_bytes(b'Binary file contents') +20 +>>> p.read_bytes() +b'Binary file contents' +</pre> <p>An existing file of the same name is overwritten.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="pathlib.Path.write_text"> +<code>Path.write_text(data, encoding=None, errors=None, newline=None)</code> </dt> <dd> +<p>Open the file pointed to in text mode, write <em>data</em> to it, and close the file:</p> <pre data-language="python">>>> p = Path('my_text_file') +>>> p.write_text('Text file contents') +18 +>>> p.read_text() +'Text file contents' +</pre> <p>An existing file of the same name is overwritten. The optional parameters have the same meaning as in <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>newline</em> parameter was added.</p> </div> </dd> +</dl> </section> </section> <section id="correspondence-to-tools-in-the-os-module"> <h2>Correspondence to tools in the os module</h2> <p>Below is a table mapping various <a class="reference internal" href="os#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> functions to their corresponding <a class="reference internal" href="#pathlib.PurePath" title="pathlib.PurePath"><code>PurePath</code></a>/<a class="reference internal" href="#pathlib.Path" title="pathlib.Path"><code>Path</code></a> equivalent.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Not all pairs of functions/methods below are equivalent. Some of them, despite having some overlapping use-cases, have different semantics. They include <a class="reference internal" href="os.path#os.path.abspath" title="os.path.abspath"><code>os.path.abspath()</code></a> and <a class="reference internal" href="#pathlib.Path.absolute" title="pathlib.Path.absolute"><code>Path.absolute()</code></a>, <a class="reference internal" href="os.path#os.path.relpath" title="os.path.relpath"><code>os.path.relpath()</code></a> and <a class="reference internal" href="#pathlib.PurePath.relative_to" title="pathlib.PurePath.relative_to"><code>PurePath.relative_to()</code></a>.</p> </div> <table class="docutils align-default"> <thead> <tr> +<th class="head"><p><a class="reference internal" href="os#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> and <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a></p></th> <th class="head"><p><a class="reference internal" href="#module-pathlib" title="pathlib: Object-oriented filesystem paths"><code>pathlib</code></a></p></th> </tr> </thead> <tr> +<td><p><a class="reference internal" href="os.path#os.path.abspath" title="os.path.abspath"><code>os.path.abspath()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.absolute" title="pathlib.Path.absolute"><code>Path.absolute()</code></a> <a class="footnote-reference brackets" href="#id5" id="id3">1</a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.realpath" title="os.path.realpath"><code>os.path.realpath()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.resolve" title="pathlib.Path.resolve"><code>Path.resolve()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.chmod" title="os.chmod"><code>os.chmod()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.chmod" title="pathlib.Path.chmod"><code>Path.chmod()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.mkdir" title="os.mkdir"><code>os.mkdir()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.mkdir" title="pathlib.Path.mkdir"><code>Path.mkdir()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.makedirs" title="os.makedirs"><code>os.makedirs()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.mkdir" title="pathlib.Path.mkdir"><code>Path.mkdir()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.rename" title="os.rename"><code>os.rename()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.rename" title="pathlib.Path.rename"><code>Path.rename()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.replace" title="os.replace"><code>os.replace()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.replace" title="pathlib.Path.replace"><code>Path.replace()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.rmdir" title="os.rmdir"><code>os.rmdir()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.rmdir" title="pathlib.Path.rmdir"><code>Path.rmdir()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.remove" title="os.remove"><code>os.remove()</code></a>, <a class="reference internal" href="os#os.unlink" title="os.unlink"><code>os.unlink()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.unlink" title="pathlib.Path.unlink"><code>Path.unlink()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.getcwd" title="os.getcwd"><code>os.getcwd()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.cwd" title="pathlib.Path.cwd"><code>Path.cwd()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.exists" title="os.path.exists"><code>os.path.exists()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.exists" title="pathlib.Path.exists"><code>Path.exists()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.expanduser" title="os.path.expanduser"><code>os.path.expanduser()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.expanduser" title="pathlib.Path.expanduser"><code>Path.expanduser()</code></a> and <a class="reference internal" href="#pathlib.Path.home" title="pathlib.Path.home"><code>Path.home()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.listdir" title="os.listdir"><code>os.listdir()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.iterdir" title="pathlib.Path.iterdir"><code>Path.iterdir()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.walk" title="os.walk"><code>os.walk()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.walk" title="pathlib.Path.walk"><code>Path.walk()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.isdir" title="os.path.isdir"><code>os.path.isdir()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.is_dir" title="pathlib.Path.is_dir"><code>Path.is_dir()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.isfile" title="os.path.isfile"><code>os.path.isfile()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.is_file" title="pathlib.Path.is_file"><code>Path.is_file()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.islink" title="os.path.islink"><code>os.path.islink()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.is_symlink" title="pathlib.Path.is_symlink"><code>Path.is_symlink()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.link" title="os.link"><code>os.link()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.hardlink_to" title="pathlib.Path.hardlink_to"><code>Path.hardlink_to()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.symlink" title="os.symlink"><code>os.symlink()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.symlink_to" title="pathlib.Path.symlink_to"><code>Path.symlink_to()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.readlink" title="os.readlink"><code>os.readlink()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.readlink" title="pathlib.Path.readlink"><code>Path.readlink()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.relpath" title="os.path.relpath"><code>os.path.relpath()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.PurePath.relative_to" title="pathlib.PurePath.relative_to"><code>PurePath.relative_to()</code></a> <a class="footnote-reference brackets" href="#id6" id="id4">2</a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os#os.stat" title="os.stat"><code>os.stat()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.stat" title="pathlib.Path.stat"><code>Path.stat()</code></a>, <a class="reference internal" href="#pathlib.Path.owner" title="pathlib.Path.owner"><code>Path.owner()</code></a>, <a class="reference internal" href="#pathlib.Path.group" title="pathlib.Path.group"><code>Path.group()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.isabs" title="os.path.isabs"><code>os.path.isabs()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.PurePath.is_absolute" title="pathlib.PurePath.is_absolute"><code>PurePath.is_absolute()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.join" title="os.path.join"><code>os.path.join()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.PurePath.joinpath" title="pathlib.PurePath.joinpath"><code>PurePath.joinpath()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.basename" title="os.path.basename"><code>os.path.basename()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.PurePath.name" title="pathlib.PurePath.name"><code>PurePath.name</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.dirname" title="os.path.dirname"><code>os.path.dirname()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.PurePath.parent" title="pathlib.PurePath.parent"><code>PurePath.parent</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.samefile" title="os.path.samefile"><code>os.path.samefile()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.Path.samefile" title="pathlib.Path.samefile"><code>Path.samefile()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="os.path#os.path.splitext" title="os.path.splitext"><code>os.path.splitext()</code></a></p></td> <td><p><a class="reference internal" href="#pathlib.PurePath.stem" title="pathlib.PurePath.stem"><code>PurePath.stem</code></a> and <a class="reference internal" href="#pathlib.PurePath.suffix" title="pathlib.PurePath.suffix"><code>PurePath.suffix</code></a></p></td> </tr> </table> <h4 class="rubric">Footnotes</h4> <dl class="footnote brackets"> <dt class="label" id="id5"> +<code>1</code> </dt> <dd> +<p><a class="reference internal" href="os.path#os.path.abspath" title="os.path.abspath"><code>os.path.abspath()</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>Path.absolute()</code></a> does not.</p> </dd> <dt class="label" id="id6"> +<code>2</code> </dt> <dd> +<p><a class="reference internal" href="#pathlib.PurePath.relative_to" title="pathlib.PurePath.relative_to"><code>PurePath.relative_to()</code></a> requires <code>self</code> to be the subpath of the argument, but <a class="reference internal" href="os.path#os.path.relpath" title="os.path.relpath"><code>os.path.relpath()</code></a> does not.</p> </dd> </dl> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/pathlib.html" class="_attribution-link">https://docs.python.org/3.12/library/pathlib.html</a> + </p> +</div> |
