pathlib模块使用

官方文档


常见使用方法: 

from pathlib import Path

'''得到当前目录的绝对路径'''
Path.cwd()
'''得到一个目录的绝对路径'''
a = Path(../XXX/XX)
a.resolve()
'''列出子目录''' p = Path('.') [x for x in p.iterdir() if x.is_dir()] '''在此目录树中列出 Python 源文件''' list(p.glob('**/*.py')) '''在目录树中导航''' p = Path('/etc') q = p / 'init.d' / 'reboot' '''查询路径是否存在''' q.exists() '''是否为目录''' q.is_dir()

与os模块的对应关系:

  

 

 

 

 

 

 

posted @ 2021-01-21 10:44  Pio-GD  阅读(60)  评论(0编辑  收藏  举报