1. # 获取当前工作目录
  2. print(Path.cwd()) # /Users/zaygee/work_script/Script/python_test/round_two
  3. # 获取当前文件路径
  4. print(Path(__file__).resolve()) # /Users/zaygee/work_script/Script/python_test/round_two/8_28_test.py
  5. # 获取用户home目录
  6. print(Path.home()) # /Users/zaygee
  7. # 获取文件属性
  8. print(Path(__file__).stat())
  9. print(Path(__file__).stat().st_mode)

文件路径组成部分

  1. .name:文件名
  2. .stem:文件名,不包含后缀
  3. .suffix:后缀
  4. .parent:父级目录
  5. .anchor:锚
  6. .parents:获取所有父级目录
  1. print(Path(__file__).name) # 8_28_test.py
  2. print(Path(__file__).stem) # 8_28_test
  3. print(Path(__file__).suffix) # .py
  4. print(Path(__file__).parent) # /Users/zaygee/work_script/Script/python_test/round_two
  5. print(Path(__file__).parent.parent) # /Users/zaygee/work_script/Script/python_test
  6. print(Path(__file__).anchor) # /
  7. print(list(Path(__file__).resolve().parents))
  8. print(Path(__file__).resolve().parents[1]) # /Users/zaygee/work_script/Script/python_test

子路径扫描

  1. print(f"扫描目录{Path.cwd()}下的所有目录")
  2. print([path for path in Path.cwd().iterdir() if Path.cwd().is_dir()])
  3. """
  4. 扫描目录/Users/zaygee/work_script/Script/python_test/round_two下的所有目录
  5. [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')]
  6. """


子目录匹配

  1. """递归所有子目录下匹配指定的目录"""
  2. for file in Path().cwd().rglob('*.py'):
  3. print(file)
  4. """
  5. /Users/zaygee/work_script/Script/python_test/round_two/8_26_test.py
  6. /Users/zaygee/work_script/Script/python_test/round_two/8_19_test.py
  7. /Users/zaygee/work_script/Script/python_test/round_two/7_25_test.py
  8. /Users/zaygee/work_script/Script/python_test/round_two/8_15_3_test.py
  9. /Users/zaygee/work_script/Script/python_test/round_two/7_8_test.py
  10. /Users/zaygee/work_script/Script/python_test/round_two/8_15_2_test.py
  11. /Users/zaygee/work_script/Script/python_test/round_two/7_12_test.py
  12. /Users/zaygee/work_script/Script/python_test/round_two/7_9_test.py
  13. """

路径拼接

  1. """路径拼接"""
  2. print(Path.home().joinpath('test', 'test.txt'))
  3. """
  4. /Users/zaygee/test/test.txt
  5. """


路径检测

  1. .is_file():是否是文件
  2. .is_dir():是否是文件夹
  3. .exists():是否存在
  1. print(Path.cwd().is_file())
  2. print(Path.cwd().is_dir())
  3. print(Path.cwd().exists())


文件操作

  1. 新增文件:touch(exist_ok=True)
  2. 新增目录:mkdir(parents=True)
  3. 删除文件:unlink()
  4. 删除目录:rmdir()
  5. 文件移动:replace()
  1. print(Path('touch.txt').touch(exist_ok=True))
  2. print(Path('mkdir').rmdir())
  3. print(Path('mkdir').mkdir())
  4. print(Path('touch.txt').unlink())
  5. 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