pathlib2 介绍
Python3时代后,Python3的系统标准库pathlib模块的 Path 对路径的操作会更简单。甚至可以说pathlib已经可以完全替代os.path,它完全采用面向对象的编程方式
官网:https://pypi.org/project/pathlib2/
pip install pathlib2
pathlib模块中类的组织结构:
- PurePath类:会将路径看做是一个普通的字符串,它可以实现将多个指定的字符串拼接成适用于当前操作系统的路径格式,同时还可以判断任意两个路径是否相等。注意,使用 PurePath 操作的路径,它并不会关心该路径是否真实有效。
- Path类:它操作的路径一定是真实有效的。Path 类提供了判断路径是否真实存在的方法。
返回当前工作目录路径和Home路径
>>tree /F
├─demo_pathlib2
│ │ main.py
│ │ __init__.py
│ │
│ ├─s1
│ │ │ s1_test.py
│ │ │ __init__.py
│ │ │
│ │ └─s11
│ │ s11_test.py
│ │ __init__.py
│ │
│ └─s2
│ s2_test.py
│ __init__.py
main.py:
from pathlib2 import Path
# eg.获取当前目录
current_path = Path.cwd()
print(current_path)
# out: WindowsPath('E:/workspace/rftestapi/aitest/demo_pathlib2')
# eg.获取父级目录
print(current_path.parent)
# out: WindowsPath('E:/workspace/rftestapi/aitest')
print(current_path.parent.parent)
# out: WindowsPath('E:/workspace/rftestapi')
# eg.获取父级目录列表
print(list(current_path.parents))
# out: [WindowsPath('E:/workspace/rftestapi/aitest'), WindowsPath('E:/workspace/rftestapi'), WindowsPath('E:/workspace'), WindowsPath('E:/')]
# eg.获取home目录
print(Path.home())
# out: WindowsPath('C:/Users/XXX')
# . 当前路径
p = Path('.')
# eg.查看文件信息
print(p.stat())
# out:os.stat_result(st_mode=16895, st_ino=10696049115192782, st_dev=1825476507, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1640925697, st_mtime=1640925697, st_ctime=1640920136)
# eg. 在目录树中导航(路径拼接)
# Path.resolve() 返回文件的绝对路径
# 方法1:
q = p / 's1' / 's1_test.py'
print(p.resolve())
# out: WindowsPath('E:/workspace/rftestapi/aitest/demo_pathlib2')
print(q.resolve())
# out: WindowsPath('E:/workspace/rftestapi/aitest/demo_pathlib2/s1/s1_test.py')
# 方法2:
q2 = p.joinpath('s1', 's1_test.py')
print(q2.resolve())
# out: WindowsPath('E:/workspace/rftestapi/aitest/demo_pathlib2/s1/s1_test.py')
# eg. 判断路径是否存在
print(q.exists())
# out: True
# eg. 判断路径是否文件夹
print(q.is_dir())
# out: False
# eg.判断路径是否文件
print(q.is_file())
# out: True
# eg.获取文件后缀
print(q.suffix)
# out: '.py'
print(q.suffixes)
# out: ['.py']
# eg.获取文件名
print(q.name)
# out: 's1_test.py'
# eg.返回目录中最后一个部分的文件名(但是不包含后缀)
print(q.stem)
# out: 's1_test'
# eg.替换目录最后一个部分的文件名并返回一个新的路径
print(q.with_name('s1_test_2.py'))
# out: WindowsPath('s1/s1_test_2.py')
# eg. 替换文件后缀并返回一个新的路径
print(q.with_suffix('.txt'))
# out: WindowsPath('s1/s1_test.txt')
# eg.获取当前目录下的文件夹
# Path.iterdir() 当前目录下的文件夹和文件生成器
# Path.is_dir() 判断是否文件夹
# Path.is_file() 判断是否文件
print([x for x in p.iterdir() if x.is_dir()])
# out:[WindowsPath('s1'), WindowsPath('s2')]
# eg.递归列出目录下所有python源文件
# list(Path.glob('**/*')) 递归目录下所有文件和文件夹
print(list(p.glob('**/*.py')))
# out: [WindowsPath('main.py'), WindowsPath('__init__.py'), WindowsPath('s1/s1_test.py'), WindowsPath('s1/__init__.py'), WindowsPath('s1/s11/s11_test.py'), WindowsPath('s1/s11/__init__.py'), WindowsPath('s2/s2_test.py'), WindowsPath('s2/__init__.py')]
# eg.打开文件
with q.open(encoding='utf-8') as f:
print(f.read())
# f.read_text() #以文本模式打开路径并并以字符串形式返回内容
# .read_bytes(): 以二进制 / 字节模式打开路径并以字节串的形式返回内容。
# .write_text(): 打开路径并向其写入字符串数据。
# .write_bytes(): 以二进制 / 字节模式打开路径并向其写入数据。
# eg. mkdir创建文件夹,mkdir方法接收两个参数:
# parents:如果父目录不存在,是否创建父目录。
# exist_ok:只有在目录不存在时创建目录,目录已存在时不会抛出异常。
q_new = p.joinpath("s3", "s33")
q_new.mkdir(parents=True, exist_ok=True)
# eg. rmdir删除文件夹
# 删除路径对象目录,如果要删除的文件夹内包含文件就会报错
q_new.rmdir()
qr = Path("E:/workspace/rftestapi/aitest/demo_pathlib2/s1/s1_test.py")
# eg. 返回路径字符串中所包含的各部分。
print(qr.parts)
# out: ('E:\\', 'workspace', 'rftestapi', 'aitest', 'demo_pathlib2', 's1', 's1_test.py')
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。