pathlib 是一个面向对象的文件系统路径模块。下面是我是用较多的函数和使用方法:
快速食用方法
导入模块
from pathlib import Path
新建一个Path对象
dstbinPath = Path(inputPath)
获取文件所在的相对路径的上级路径
rootPath = Path.cwd().parent
parent属性可以链式调用,比如
Path.cwd().parent.parent.parent.parent
Path对象的parents属性可以拿到各级目录列表(索引值越大越接近root)
拼接路径
logPath = Path.joinpath(rootPath, 'errorlog')
遍历文件夹文件
for sub in logPath.iterdir():
if sub.is_file():
# 获取文件的绝对路径
srcPath = sub.resolve()
判断文件后缀扩展名
if logfile.suffix == '.json':
删除文件
file.unlink()