pathlib
— 面向对象的文件系统路径
¶
3.4 版新增。
源代码: Lib/pathlib.py
此模块提供表示文件系统路径的类,采用适合不同操作系统的语义。路径类分为 纯路径 ,提供没有 I/O 的纯计算操作,和 具体路径 ,继承自纯路径但还提供 I/O 操作。
若之前从未用过此模块,或仅仅不确定哪个类适合任务,
Path
更可能是您需要的。它实例化
具体路径
对于代码所运行的平台。
纯路径在某些特殊情况下很有用;例如:
若想要在 Unix 机器中操纵 Windows 路径 (反之亦然)。不能实例化
WindowsPath
当运行在 Unix 时,但可以实例化
PureWindowsPath
.
想要确保代码只操纵路径而不实际访问操作系统。在这种情况下,实例化一个纯类可能是有用的,因为它们没有任何操作系统访问操作。
另请参阅
PEP 428 :pathlib 模块 – 面向对象的文件系统路径。
另请参阅
对于低级字符串路径操纵,还可以使用
os.path
模块。
导入主类:
>>> from pathlib import Path
列出子目录:
>>> p = Path('.') >>> [x for x in p.iterdir() if x.is_dir()] [PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'), PosixPath('__pycache__'), PosixPath('build')]
列表此目录树下的 Python 源文件:
>>> list(p.glob('**/*.py')) [PosixPath('test_pathlib.py'), PosixPath('setup.py'), PosixPath('pathlib.py'), PosixPath('docs/conf.py'), PosixPath('build/lib/pathlib.py')]
在目录树中导航:
>>> p = Path('/etc') >>> q = p / 'init.d' / 'reboot' >>> q PosixPath('/etc/init.d/reboot') >>> q.resolve() PosixPath('/etc/rc.d/init.d/halt')
查询路径特性:
>>> q.exists() True >>> q.is_dir() False
打开文件:
>>> with q.open() as f: f.readline() ... '#!/bin/bash\n'
纯路径对象提供不实际访问文件系统的路径处理操作。访问这些类有 3 种方式,也称为 flavours :
表示系统路径风味的一般类 (实例化它会创建
PurePosixPath
或
PureWindowsPath
):
>>> PurePath('setup.py') # Running on a Unix machine PurePosixPath('setup.py')
每元素的
pathsegments
可以是表示路径段的字符串,对象实现
os.PathLike
接口返回字符串,或另一路径对象:
>>> PurePath('foo', 'some/path', 'bar') PurePosixPath('foo/some/path/bar') >>> PurePath(Path('foo'), Path('bar')) PurePosixPath('foo/bar')
当 pathsegments 为空,则假定当前目录:
>>> PurePath() PurePosixPath('.')
当给出几个绝对路径时,采用最后路径作为锚 (模仿
os.path.join()
的行为):
>>> PurePath('/etc', '/usr', 'lib64') PurePosixPath('/usr/lib64') >>> PureWindowsPath('c:/Windows', 'd:bar') PureWindowsPath('d:bar')
不管怎样,在 Windows 路径,更改本地根目录不会丢弃先前的驱动设置:
>>> PureWindowsPath('c:/Windows', '/Program Files') PureWindowsPath('c:/Program Files')
伪斜杠和单点会被折叠,但双点 (
'..'
) and leading double slashes (
'//'
) are not, since this would change the meaning of a path for various reasons (e.g. symbolic links, UNC paths):
>>> PurePath('foo//bar') PurePosixPath('foo/bar') >>> PurePath('//foo/bar') PurePosixPath('//foo/bar') >>> PurePath('foo/./bar') PurePosixPath('foo/bar') >>> PurePath('foo/../bar') PurePosixPath('foo/../bar')
(天真方式将使
PurePosixPath('foo/../bar')
相当于
PurePosixPath('bar')
,这是错的若
foo
是到另一目录的符号链接)
纯路径对象实现
os.PathLike
接口,允许它们在接受接口的任何地方使用。
3.6 版改变:
添加支持
os.PathLike
接口。
子类化的
PurePath
,此路径风味表示非 Windows 文件系统路径:
>>> PurePosixPath('/etc') PurePosixPath('/etc')
pathsegments
的指定类似于
PurePath
.
子类化的
PurePath
, this path flavour represents Windows filesystem paths, including
UNC paths
:
>>> PureWindowsPath('c:/Program Files/') PureWindowsPath('c:/Program Files') >>> PureWindowsPath('//server/share/file') PureWindowsPath('//server/share/file')
pathsegments
的指定类似于
PurePath
.
不管在哪个系统中运行,可以实例化所有这些类,因为它们不提供任何做系统调用的操作。
路径不可变且可哈希。相同风味的路径可比较且可排序。这些特性遵守风味的大小写折叠语义:
>>> PurePosixPath('foo') == PurePosixPath('FOO') False >>> PureWindowsPath('foo') == PureWindowsPath('FOO') True >>> PureWindowsPath('FOO') in { PureWindowsPath('foo') } True >>> PureWindowsPath('C:') < PureWindowsPath('d:') True
不同风味的路径比较不相等且无法排序:
>>> 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'
斜杠运算符帮助创建子级路径,类似于
os.path.join()
:
>>> p = PurePath('/etc') >>> p PurePosixPath('/etc') >>> p / 'init.d' / 'apache2' PurePosixPath('/etc/init.d/apache2') >>> q = PurePath('bin') >>> '/usr' / q PurePosixPath('/usr/bin')
路径对象可用于任何地方的对象实现
os.PathLike
被接受:
>>> import os >>> p = PurePath('/etc') >>> os.fspath(p) '/etc'
路径的字符串表示是原生文件系统路径本身 (按本机形式,如在 Windows 采用反斜杠),可以将其传递给任何按字符串接受文件路径的函数:
>>> p = PurePath('/etc') >>> str(p) '/etc' >>> p = PureWindowsPath('c:/Program Files') >>> str(p) 'c:\\Program Files'
同样,调用
bytes
在路径给出原生文件系统路径作为字节对象,编码通过
os.fsencode()
:
>>> bytes(p) b'/etc'
注意
调用
bytes
仅推荐在 Unix。在 Windows,Unicode 形式是文件系统路径的典型表示。
要访问路径的各个部分 (组件),使用以下特性:
给出访问路径各组件的元组:
>>> p = PurePath('/usr/bin/python3') >>> p.parts ('/', 'usr', 'bin', 'python3') >>> p = PureWindowsPath('c:/Program Files/PSF') >>> p.parts ('c:\\', 'Program Files', 'PSF')
(注意,驱动和本地根是如何重新分组在一部分的)
纯路径提供以下方法和特性:
A string representing the drive letter or name, if any:
>>> PureWindowsPath('c:/Program Files/').drive 'c:' >>> PureWindowsPath('/Program Files/').drive '' >>> PurePosixPath('/etc').drive ''
UNC shares are also considered drives:
>>> PureWindowsPath('//host/share/foo.txt').drive '\\\\host\\share'
A string representing the (local or global) root, if any:
>>> PureWindowsPath('c:/Program Files/').root '\\' >>> PureWindowsPath('c:Program Files/').root '' >>> PurePosixPath('/etc').root '/'
UNC shares always have a root:
>>> PureWindowsPath('//host/share').root '\\'
If the path starts with more than two successive slashes,
PurePosixPath
collapses them:
>>> PurePosixPath('//etc').root '//' >>> PurePosixPath('///etc').root '/' >>> PurePosixPath('////etc').root '/'
注意
This behavior conforms to The Open Group Base Specifications Issue 6 , paragraph 4.11 Pathname Resolution :
“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.”
The concatenation of the drive and root:
>>> PureWindowsPath('c:/Program Files/').anchor 'c:\\' >>> PureWindowsPath('c:Program Files/').anchor 'c:' >>> PurePosixPath('/etc').anchor '/' >>> PureWindowsPath('//host/share').anchor '\\\\host\\share\\'
An immutable sequence providing access to the logical ancestors of the path:
>>> 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:/')
3.10 版改变: The parents sequence now supports slices and negative index values.
路径的逻辑父级:
>>> p = PurePosixPath('/a/b/c/d') >>> p.parent PurePosixPath('/a/b/c')
You cannot go past an anchor, or empty path:
>>> p = PurePosixPath('/') >>> p.parent PurePosixPath('/') >>> p = PurePosixPath('.') >>> p.parent PurePosixPath('.')
注意
This is a purely lexical operation, hence the following behaviour:
>>> p = PurePosixPath('foo/..') >>> p.parent PurePosixPath('foo')
If you want to walk an arbitrary filesystem path upwards, it is recommended to first call
Path.resolve()
so as to resolve symlinks and eliminate
“..”
components.
A string representing the final path component, excluding the drive and root, if any:
>>> PurePosixPath('my/library/setup.py').name 'setup.py'
UNC drive names are not considered:
>>> PureWindowsPath('//some/share/setup.py').name 'setup.py' >>> PureWindowsPath('//some/share').name ''
The file extension of the final component, if any:
>>> PurePosixPath('my/library/setup.py').suffix '.py' >>> PurePosixPath('my/library.tar.gz').suffix '.gz' >>> PurePosixPath('my/library').suffix ''
A list of the path’s file extensions:
>>> PurePosixPath('my/library.tar.gar').suffixes ['.tar', '.gar'] >>> PurePosixPath('my/library.tar.gz').suffixes ['.tar', '.gz'] >>> PurePosixPath('my/library').suffixes []
The final path component, without its suffix:
>>> PurePosixPath('my/library.tar.gz').stem 'library.tar' >>> PurePosixPath('my/library.tar').stem 'library' >>> PurePosixPath('my/library').stem 'library'
Return a string representation of the path with forward slashes (
/
):
>>> p = PureWindowsPath('c:\\windows') >>> str(p) 'c:\\windows' >>> p.as_posix() 'c:/windows'
将路径表示为
file
URI.
ValueError
被引发若不是绝对路径。
>>> p = PurePosixPath('/etc/passwd') >>> p.as_uri() 'file:///etc/passwd' >>> p = PureWindowsPath('c:/Windows') >>> p.as_uri() 'file:///c:/Windows'
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:
>>> 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
Return whether or not this path is relative to the other 路径。
>>> p = PurePath('/etc/passwd') >>> p.is_relative_to('/etc') True >>> p.is_relative_to('/usr') False
3.9 版新增。
采用
PureWindowsPath
,返回
True
if the path is considered reserved under Windows,
False
otherwise. With
PurePosixPath
,
False
is always returned.
>>> PureWindowsPath('nul').is_reserved() True >>> PurePosixPath('nul').is_reserved() False
File system calls on reserved paths can fail mysteriously or have unintended effects.
Calling this method is equivalent to combining the path with each of the other arguments in turn:
>>> 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')
Match this path against the provided glob-style pattern. Return
True
if matching is successful,
False
否则。
若 pattern is relative, the path can be either relative or absolute, and matching is done from the right:
>>> 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
若 pattern is absolute, the path must be absolute, and the whole path must match:
>>> PurePath('/a.py').match('/*.py') True >>> PurePath('a/b.py').match('/*.py') False
As with other methods, case-sensitivity follows platform defaults:
>>> PurePosixPath('b.py').match('*.PY') False >>> PureWindowsPath('b.py').match('*.PY') True
Compute a version of this path relative to the path represented by other . If it’s impossible, ValueError is raised:
>>> 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 694, in relative_to .format(str(self), str(formatted))) ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other absolute.
NOTE: This function is part of
PurePath
and works with strings. It does not check or access the underlying file structure.
返回的新路径带
name
changed. If the original path doesn’t have a name, ValueError is raised:
>>> 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
返回的新路径带
stem
changed. If the original path doesn’t have a name, ValueError is raised:
>>> 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
3.9 版新增。
返回的新路径带
suffix
changed. If the original path doesn’t have a suffix, the new
suffix
is appended instead. If the
suffix
is an empty string, the original suffix is removed:
>>> 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')
具体路径是纯路径类的子类。除后者提供的操作外,它们还提供对路径对象做系统调用的方法。实例化具体路径有 3 种方式:
子类化的
PurePath
,此类表示系统路径风味的具体路径 (实例化它创建
PosixPath
或
WindowsPath
):
>>> Path('setup.py') PosixPath('setup.py')
pathsegments
的指定类似于
PurePath
.
子类化的
Path
and
PurePosixPath
,此类表示非 Windows 文件系统具体路径:
>>> PosixPath('/etc') PosixPath('/etc')
pathsegments
的指定类似于
PurePath
.
子类化的
Path
and
PureWindowsPath
,此类表示 Windows 文件系统具体路径:
>>> WindowsPath('c:/Program Files/') WindowsPath('c:/Program Files')
pathsegments
的指定类似于
PurePath
.
只可以实例化对应系统风味的类 (允许系统调用不兼容风味路径,会导致应用程序 Bug 或故障):
>>> 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
Concrete paths provide the following methods in addition to pure paths methods. Many of these methods can raise an
OSError
if a system call fails (for example because the path doesn’t exist).
3.8 版改变:
exists()
,
is_dir()
,
is_file()
,
is_mount()
,
is_symlink()
,
is_block_device()
,
is_char_device()
,
is_fifo()
,
is_socket()
现在返回
False
instead of raising an exception for paths that contain characters unrepresentable at the OS level.
返回表示当前目录的新路径对象 (如返回通过
os.getcwd()
):
>>> Path.cwd() PosixPath('/home/antoine/pathlib')
返回表示用户 Home (主) 目录的新路径对象 (如返回通过
os.path.expanduser()
with
~
construct). If the home directory can’t be resolved,
RuntimeError
被引发。
>>> Path.home() PosixPath('/home/antoine')
3.5 版新增。
返回
os.stat_result
对象包含此路径的有关信息,像
os.stat()
. The result is looked up at each call to this method.
This method normally follows symlinks; to stat a symlink add the argument
follow_symlinks=False
,或使用
lstat()
.
>>> p = Path('setup.py') >>> p.stat().st_size 956 >>> p.stat().st_mtime 1327883547.852554
3.10 版改变: follow_symlinks 参数被添加。
更改文件模式和权限,像
os.chmod()
.
This method normally follows symlinks. Some Unix flavours support changing permissions on the symlink itself; on these platforms you may add the argument
follow_symlinks=False
,或使用
lchmod()
.
>>> p = Path('setup.py') >>> p.stat().st_mode 33277 >>> p.chmod(0o444) >>> p.stat().st_mode 33060
3.10 版改变: follow_symlinks 参数被添加。
路径是否指向现有文件或目录:
>>> Path('.').exists() True >>> Path('setup.py').exists() True >>> Path('/etc').exists() True >>> Path('nonexistentfile').exists() False
注意
若路径指向符号链接,
exists()
returns whether the symlink
points to
an existing file or directory.
Return a new path with expanded
~
and
~user
constructs, as returned by
os.path.expanduser()
. If a home directory can’t be resolved,
RuntimeError
被引发。
>>> p = PosixPath('~/films/Monty Python') >>> p.expanduser() PosixPath('/home/eric/films/Monty Python')
3.5 版新增。
Glob the given relative pattern in the directory represented by this path, yielding all matching files (of any kind):
>>> sorted(Path('.').glob('*.py')) [PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')] >>> sorted(Path('.').glob('*/*.py')) [PosixPath('docs/conf.py')]
Patterns are the same as for
fnmatch
, with the addition of “
**
” which means “this directory and all subdirectories, recursively”. In other words, it enables recursive globbing:
>>> sorted(Path('.').glob('**/*.py')) [PosixPath('build/lib/pathlib.py'), PosixPath('docs/conf.py'), PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]
注意
使用
**
模式在大型目录树中可能消耗过多时间。
引发
审计事件
pathlib.Path.glob
采用自变量
self
,
pattern
.
3.11 版改变:
Return only directories if
pattern
ends with a pathname components separator (
sep
or
altsep
).
Return the name of the group owning the file.
KeyError
is raised if the file’s gid isn’t found in the system database.
返回
True
if the path points to a directory (or a symbolic link pointing to a directory),
False
if it points to another kind of file.
False
is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
返回
True
if the path points to a regular file (or a symbolic link pointing to a regular file),
False
if it points to another kind of file.
False
is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
返回
True
if the path is a
mount point
: a point in a file system where a different file system has been mounted. On POSIX, the function checks whether
path
’s parent,
path/..
, is on a different device than
path
, or whether
path/..
and
path
point to the same i-node on the same device — this should detect mount points for all Unix and POSIX variants. Not implemented on Windows.
3.7 版新增。
返回
True
if the path points to a symbolic link,
False
否则。
False
is also returned if the path doesn’t exist; other errors (such as permission errors) are propagated.
返回
True
if the path points to a Unix socket (or a symbolic link pointing to a Unix socket),
False
if it points to another kind of file.
False
is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
返回
True
if the path points to a FIFO (or a symbolic link pointing to a FIFO),
False
if it points to another kind of file.
False
is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
返回
True
if the path points to a block device (or a symbolic link pointing to a block device),
False
if it points to another kind of file.
False
is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
返回
True
if the path points to a character device (or a symbolic link pointing to a character device),
False
if it points to another kind of file.
False
is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
When the path points to a directory, yield path objects of the directory contents:
>>> 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')
The children are yielded in arbitrary order, and the special entries
'.'
and
'..'
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.
像
Path.chmod()
but, if the path points to a symbolic link, the symbolic link’s mode is changed rather than its target’s.
像
Path.stat()
but, if the path points to a symbolic link, return the symbolic link’s information rather than its target’s.
Create a new directory at this given path. If
mode
is given, it is combined with the process’
umask
value to determine the file mode and access flags. If the path already exists,
FileExistsError
被引发。
若
parents
is true, any missing parents of this path are created as needed; they are created with the default permissions without taking
mode
into account (mimicking the POSIX
mkdir -p
命令)。
若
parents
is false (the default), a missing parent raises
FileNotFoundError
.
若
exist_ok
为 False (默认),
FileExistsError
被引发若目标目录已存在。
若
exist_ok
为 True,
FileExistsError
异常将被忽略 (相同行为如 POSIX
mkdir -p
command), but only if the last path component is not an existing non-directory file.
3.5 版改变: exist_ok 参数被添加。
Open the file pointed to by the path, like the built-in
open()
function does:
>>> p = Path('setup.py') >>> with p.open() as f: ... f.readline() ... '#!/usr/bin/env python3\n'
Return the name of the user owning the file.
KeyError
is raised if the file’s uid isn’t found in the system database.
Return the binary contents of the pointed-to file as a bytes object:
>>> p = Path('my_binary_file') >>> p.write_bytes(b'Binary file contents') 20 >>> p.read_bytes() b'Binary file contents'
3.5 版新增。
Return the decoded contents of the pointed-to file as a string:
>>> p = Path('my_text_file') >>> p.write_text('Text file contents') 18 >>> p.read_text() 'Text file contents'
The file is opened and then closed. The optional parameters have the same meaning as in
open()
.
3.5 版新增。
Return the path to which the symbolic link points (as returned by
os.readlink()
):
>>> p = Path('mylink') >>> p.symlink_to('setup.py') >>> p.readlink() PosixPath('setup.py')
3.9 版新增。
Rename this file or directory to the given
target
, and return a new Path instance pointing to
target
. On Unix, if
target
exists and is a file, it will be replaced silently if the user has permission. On Windows, if
target
exists,
FileExistsError
会被引发。
target
can be either a string or another path object:
>>> p = Path('foo') >>> p.open('w').write('some text') 9 >>> target = Path('bar') >>> p.rename(target) PosixPath('bar') >>> target.open().read() 'some text'
The target path may be absolute or relative. Relative paths are interpreted relative to the current working directory, not the directory of the Path object.
3.8 版改变: Added return value, return the new Path instance.
Rename this file or directory to the given target , and return a new Path instance pointing to target 。若 target points to an existing file or empty directory, it will be unconditionally replaced.
The target path may be absolute or relative. Relative paths are interpreted relative to the current working directory, not the directory of the Path object.
3.8 版改变: Added return value, return the new Path instance.
Make the path absolute, without normalization or resolving symlinks. Returns a new path object:
>>> p = Path('tests') >>> p PosixPath('tests') >>> p.absolute() PosixPath('/home/antoine/pathlib/tests')
Make the path absolute, resolving any symlinks. A new path object is returned:
>>> p = Path() >>> p PosixPath('.') >>> p.resolve() PosixPath('/home/antoine/pathlib')
“
..
” components are also eliminated (this is the only method to do so):
>>> p = Path('docs/../setup.py') >>> p.resolve() PosixPath('/home/antoine/pathlib/setup.py')
若路径不存在且
strict
is
True
,
FileNotFoundError
被引发。若
strict
is
False
, 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,
RuntimeError
被引发。
3.6 版新增: strict 自变量 (3.6 之前的行为是 strict 严格)。
这就像调用
Path.glob()
with “
**/
” added in front of the given relative
pattern
:
>>> sorted(Path().rglob("*.py")) [PosixPath('build/lib/pathlib.py'), PosixPath('docs/conf.py'), PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]
引发
审计事件
pathlib.Path.rglob
采用自变量
self
,
pattern
.
3.11 版改变:
Return only directories if
pattern
ends with a pathname components separator (
sep
or
altsep
).
移除此目录。目录必须为空。
Return whether this path points to the same file as
other_path
, which can be either a Path object, or a string. The semantics are similar to
os.path.samefile()
and
os.path.samestat()
.
An
OSError
can be raised if either file cannot be accessed for some reason.
>>> p = Path('spam') >>> q = Path('eggs') >>> p.samefile(q) False >>> p.samefile('spam') True
3.5 版新增。
使此路径符号链接到
target
。在 Windows,
target_is_directory
必须是 True (默认
False
) if the link’s target is a directory. Under POSIX,
target_is_directory
’s value is ignored.
>>> 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
注意
The order of arguments (link, target) is the reverse of
os.symlink()
’s.
Make this path a hard link to the same file as target .
注意
The order of arguments (link, target) is the reverse of
os.link()
’s.
3.10 版新增。
Make target 硬链接到此路径。
警告
此函数不会使此路径硬链接到
target
, despite the implication of the function and argument names. The argument order (target, link) is the reverse of
Path.symlink_to()
and
Path.hardlink_to()
, but matches that of
os.link()
.
3.8 版新增。
从 3.10 版起弃用:
This method is deprecated in favor of
Path.hardlink_to()
, as the argument order of
Path.link_to()
does not match that of
Path.symlink_to()
.
在此给定路径创建文件。若
mode
is given, it is combined with the process’
umask
value to determine the file mode and access flags. If the file already exists, the function succeeds if
exist_ok
is true (and its modification time is updated to the current time), otherwise
FileExistsError
被引发。
移除此文件或符号链接。若路径指向目录,使用
Path.rmdir()
代替。
若
missing_ok
为 False (默认),
FileNotFoundError
被引发若路径不存在。
若
missing_ok
为 True,
FileNotFoundError
异常将被忽略 (相同行为如 POSIX
rm -f
命令)。
3.8 版改变: missing_ok 参数被添加。
Open the file pointed to in bytes mode, write data to it, and close the file:
>>> p = Path('my_binary_file') >>> p.write_bytes(b'Binary file contents') 20 >>> p.read_bytes() b'Binary file contents'
An existing file of the same name is overwritten.
3.5 版新增。
Open the file pointed to in text mode, write data to it, and close the file:
>>> p = Path('my_text_file') >>> p.write_text('Text file contents') 18 >>> p.read_text() 'Text file contents'
An existing file of the same name is overwritten. The optional parameters have the same meaning as in
open()
.
3.5 版新增。
3.10 版改变: newline 参数被添加。
os
模块
¶
下表映射各种
os
函数到其相应
PurePath
/
Path
等价。
注意
Not all pairs of functions/methods below are equivalent. Some of them, despite having some overlapping use-cases, have different semantics. They include
os.path.abspath()
and
Path.absolute()
,
os.path.relpath()
and
PurePath.relative_to()
.
脚注
os.path.abspath()
normalizes the resulting path, which may change its meaning in the presence of symlinks, while
Path.absolute()
不会。
PurePath.relative_to()
requires
self
to be the subpath of the argument, but
os.path.relpath()
不会。