比os.path更高级的路径对象pathlib

pathlib中的Path类继承自PurePath,对PurePath中的部分方法进行了重载,相比于os.path有更高的抽象级别


几个特点

  • 路径连接便捷
  • 正则匹配相关文件,返回可迭代可打开的文件对象

示例1
from pathlib import Path
root = Path("../PycharmProject")
config_dir = root/Path("config")
# 等价于config_dir = os.path.join(root, 'config')
print(str(config_dir))

Output: …/PycharmProject/config


示例2
path1 = Path("/home/unv")
path2 = Path("/home/unv/project/src")
print(path2.relative_to(path1))

Output: PosixPath(“project/src”)


正则匹配相关文件,并返回可迭代的文件对象

config.files = config_dir.rglob("*.json")

返回: [ PosixPath("/home/unv/data1.json"), PosixPath("/home/unv/data2.json")]
!可迭代


方法列表

(具体可看源码的细节)

基本用法:

Path.iterdir()  #遍历目录的子目录或者文件

Path.is_dir()  #判断是否是目录

Path.glob()  #过滤目录(返回生成器)

Path.resolve()  #返回绝对路径

拼接路径(目录中进行导航-官网说法)

Path.exists()  #判断路径是否存在

Path.open()  #打开文件(支持with)

Path.unlink()  #删除文件或目录(目录非空触发异常)


基本属性:

Path.parts  #分割路径 类似os.path.split(), 不过返回元组

Path.drive  #返回驱动器名称

Path.root  #返回路径的根目录

Path.anchor  #自动判断返回drive或root

Path.parents  #返回所有上级目录的列表


改变路径:

Path.with_name()  #更改路径名称, 更改最后一级路径名

Path.with_suffix()  #更改路径后缀

#拼接路径

Path.joinpath()  #拼接路径

Path.relative_to()  #计算相对路径


测试路径:

Path.match()  #测试路径是否符合pattern

Path.is_dir()  #是否是文件

Path.is_absolute()  #是否是绝对路径

Path.is_reserved()  #是否是预留路径

Path.exists()  #判断路径是否真实存在


其他方法:

Path.cwd()  #返回当前目录的路径对象

Path.home()  #返回当前用户的home路径对象

Path.stat()  #返回路径信息, 同os.stat()

Path.chmod()  #更改路径权限, 类似os.chmod()

Path.expanduser()  #展开~返回完整路径对象

Path.mkdir()  #创建目录

Path.rename()  #重命名路径

Path.rglob()  #递归遍历所有子目录的文件


参考:[pathlib]内置pathlib库的常用属性和方法

引用\[3\]中的程序演示了os.path模块中一些函数的功能和用法。如果要将os.path.split函数替换为pathlib模块中的相应函数,可以使用pathlib.Path对象的parent属性和name属性来实现相同的功能。 例如,将os.path.split('/root/workspace/python3_learning/test.py')替换为pathlib模块的代码可以是: ```python import pathlib path = pathlib.Path('/root/workspace/python3_learning/test.py') parent_dir = path.parent file_name = path.name print(parent_dir) # 输出:/root/workspace/python3_learning print(file_name) # 输出:test.py ``` 这样就可以获取到指定路径的目录和文件名,实现了os.path.split函数的功能。 #### 引用[.reference_title] - *1* *2* [Pythonos.path() 和 pathlib.Path()](https://blog.csdn.net/TomorrowAndTuture/article/details/113582588)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python os.path模块常见函数用法(实例+详细注释)](https://blog.csdn.net/ccc369639963/article/details/124012787)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值