-
- pathlib.PurePath
- 分割路径 parts
- 驱动器 drive
- 根目录 root
- 自动判断驱动器或根目录 anchor
- 所有上级目录列表 parents
- 父目录 parent
- 完整文件名 name
- 文件后缀 suffix
- 文件后缀列表 suffixes
- 文件名 stem
- Unix 路径分隔符表示 as_posix()
- 文件 URI 表示 as_uri()
- 判断绝对路径 is_absolute()
- 拼接路径 joinpath(*other)
- 测试路径符合模式 match(pattern)
- 计算相对路径 relative_to(*other)
- 更改路径名 with_name(name)
- 更改路径后缀 with_suffix(suffix)
- pathlib.Path
- 工作目录 Path.cwd()
- 用户家目录 Path.home()
- 路径信息 stat()
- 更改路径权限 chmod(mode)
- 判断路径存在 exists()
- 展开~完整路径 expanduser()
- 过滤目录 glob(pattern)
- 用户组 group()
- 判断目录 is_dir()
- 判断文件 is_file()
- 判断符号链接 is_symlink()
- 判断套接字 is_socket()
- 判断管道 is_fifo()
- 判断块设备 is_block_device()
- 判断字符设备 is_char_device()
- 遍历目录 iterdir()
- 更改符号链接权限 lchmod(mode)
- 符号链接路径信息 lstat()
- 创建目录 mkdir()
- 打开文件 open()
- 文件拥有者 owner()
- 按字节读取 read_bytes()
- 按字符读取 read_text()
- 重命名 rename(target)
- 跨平台重命名 replace(target)
- 解析绝对路径 resolve(strict=False)
- 过滤相对路径 rglob(pattern)
- 删除空目录 rmdir()
- 判断同一文件 samefile(other_pattern)
- 创建符号链接 symlink_to()
- 创建文件 touch()
- 删除文件 unlink()
- 按字节写入 write_bytes(data)
- 按字符写入 write_text()
- pathlib.PurePath
pathlib
模块提供了文件路径对象抽象,不仅仅是替换 os.path
模块对文件的操作,可以说是路径处理的瑞士军刀。
This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations.
其中最常使用的就是 Path
类,因为它与具体系统无关。
pathlib.PurePath
分割路径 parts
类似 os.path.split()
,但返回元组。
p = PurePath('/usr/bin/python3').parts # ('/', 'usr', 'bin', 'python3')
PureWindowsPath('c:/Program Files/PSF').parts # ('c:\\', 'Program Files', 'PSF')
驱动器 drive
返回驱动器名称,主要面向 Windows 系统。
PureWindowsPath('c:/Program Files/').drive # 'c:'
PureWindowsPath('/Program Files/').drive # ''
PurePosixPath('/etc').drive # ''
根目录 root
返回路径的根目录,面向 Unix/Linux 系统。
PureWindowsPath('c:/Program Files/').root # '\\'
PureWindowsPath('c:Program Files/').root # ''
PureWindowsPath('//host/share').root # '\\'
PurePosixPath('/etc').root # '/'
自动判断驱动器或根目录 anchor
PureWindowsPath('c:/Program Files/').anchor # 'c:\\'
PureWindowsPath('c:Program Files/').anchor # 'c:'
PurePosixPath('/etc').anchor # '/'
PureWindowsPath('//host/share').anchor # '\\\\host\\share\\'
所有上级目录列表 parents
返回所有上级(祖先)目录列表,[上级目录, 上上级目录, …, 根目录]
PureWindowsPath('c:/foo/bar/setup.py').parents
# [PureWindowsPath('c:/foo/bar'), PureWindowsPath('c:/foo'), PureWindowsPath('c:/')]
父目录 parent
返回父目录
PurePosixPath('/a/b/c/d').parent # PurePosixPath('/a/b/c')
注意相对路径问题:
PurePosixPath('/').parent # PurePosixPath('/')
PurePosixPath('.').parent # PurePosixPath('.')
PurePosixPath('foo/..').parent # PurePosixPath('foo')
在遇到相对路径时,在获取父目录之前调用 Path.resolve()
消除符号链接和 ..
之类表示。
完整文件名 name
返回除驱动器或根目录外完整文件名(带文件名后缀)。
PurePosixPath('my/library/setup.py').name # 'setup.py'
PureWindowsPath('//some/share/setup.py').name # 'setup.py'
PureWindowsPath('//some/share').name # ''
文件后缀 suffix
返回文件扩后缀,没有扩展名则返回空串。
PurePosixPath('my/library/setup.py').suffix # '.py'
PurePosixPath('my/library.tar.gz').suffix # '.gz'
PurePosixPath('my/library').suffix # ''
文件后缀列表 suffixes
当文件有多个后缀,返回文件所有后缀列表;没有则返回空列表。
PurePosixPath('my/library.tar.gar').suffixes # ['.tar', '.gar']
PurePosixPath('my/library.tar.gz').suffixes # ['.tar', '.gz']
PurePosi