# 获取当前工作目录print(Path.cwd()) # /Users/zaygee/work_script/Script/python_test/round_two# 获取当前文件路径print(Path(__file__).resolve()) # /Users/zaygee/work_script/Script/python_test/round_two/8_28_test.py# 获取用户home目录print(Path.home()) # /Users/zaygee# 获取文件属性print(Path(__file__).stat())print(Path(__file__).stat().st_mode)
文件路径组成部分
.name:文件名 .stem:文件名,不包含后缀 .suffix:后缀 .parent:父级目录 .anchor:锚 .parents:获取所有父级目录
print(Path(__file__).name) # 8_28_test.pyprint(Path(__file__).stem) # 8_28_testprint(Path(__file__).suffix) # .pyprint(Path(__file__).parent) # /Users/zaygee/work_script/Script/python_test/round_twoprint(Path(__file__).parent.parent) # /Users/zaygee/work_script/Script/python_testprint(Path(__file__).anchor) # /print(list(Path(__file__).resolve().parents))print(Path(__file__).resolve().parents[1]) # /Users/zaygee/work_script/Script/python_test
子路径扫描
print(f"扫描目录{Path.cwd()}下的所有目录")print([path for path in Path.cwd().iterdir() if Path.cwd().is_dir()])"""扫描目录/Users/zaygee/work_script/Script/python_test/round_two下的所有目录[PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_26_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_19_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_25_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_15_3_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_8_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_15_2_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_12_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_9_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_27_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_10_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/test-log.log'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_18_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_11_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/__pycache__'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7-5-test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_14_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_28_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_6_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_7_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/mkdir'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_16_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_19_1.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_14_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_17_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/8_15_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_29_test.py'), PosixPath('/Users/zaygee/work_script/Script/python_test/round_two/7_20_test.py')]"""
子目录匹配
"""递归所有子目录下匹配指定的目录"""for file in Path().cwd().rglob('*.py'): print(file)"""/Users/zaygee/work_script/Script/python_test/round_two/8_26_test.py/Users/zaygee/work_script/Script/python_test/round_two/8_19_test.py/Users/zaygee/work_script/Script/python_test/round_two/7_25_test.py/Users/zaygee/work_script/Script/python_test/round_two/8_15_3_test.py/Users/zaygee/work_script/Script/python_test/round_two/7_8_test.py/Users/zaygee/work_script/Script/python_test/round_two/8_15_2_test.py/Users/zaygee/work_script/Script/python_test/round_two/7_12_test.py/Users/zaygee/work_script/Script/python_test/round_two/7_9_test.py"""
路径拼接
"""路径拼接"""print(Path.home().joinpath('test', 'test.txt'))"""/Users/zaygee/test/test.txt"""
路径检测
.is_file():是否是文件 .is_dir():是否是文件夹 .exists():是否存在
print(Path.cwd().is_file())print(Path.cwd().is_dir())print(Path.cwd().exists())
文件操作
新增文件:touch(exist_ok=True) 新增目录:mkdir(parents=True) 删除文件:unlink() 删除目录:rmdir() 文件移动:replace()
print(Path('touch.txt').touch(exist_ok=True))print(Path('mkdir').rmdir())print(Path('mkdir').mkdir())print(Path('touch.txt').unlink())print(Path('test.txt').replace(Path.joinpath(Path.cwd().parent, 'test.txt')))
�
常用pathlib和os对比图
| 操作 |
os |
pathlib |
| 绝对路径 |
os.path.abspath |
Path.resolve |
| 修改权限 |
os.chmod |
Path.chmod |
| 创建目录 |
os.mkdir |
Path.mkdir |
| 重命名 |
os.rename |
Path.rename |
| 移动 |
os.replace |
Path.replace |
| 删除目录 |
os.rmdir |
Path.rmdir |
| 删除文件 |
os.remove\os.unlink |
Path.unlink |
| 工作目录 |
os.getcwd |
Path.cwd |
| 用户目录 |
os.path.expanduser |
Path.home |
| 上级目录 |
os.path.dirname |
Path(file).parent |
| 是否存在 |
os.path.exists |
Path.exists |
| 是否为目录 |
os.path.isdir |
Path.is_dir |
| 是否为文件 |
os.path.isfile |
Path.is_file |
| 是否绝对路径 |
os.path.isabs |
PurePath.is_absolute |
| 路径拼接 |
os.path.join |
PurePath.joinpath |
| 文件名 |
os.path.basename |
PurePath.name |