os模块和pathlib模块方法对应一览表

os模块和pathlib模块方法对应一览表

对于习惯了使用os模块操作文件和文件夹路径的朋友来说,下表给出了一一对应的关系,建议收藏。

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.exists Path.exists
os.path.expanduser Path.expanduserand Path.home
os.path.isdir Path.is_dir
os.path.isfile Path.is_file
os.path.islink Path.is_symlink
os.stat Path.stat, Path.owner,
os.path.isabs PurePath.is_absolute
os.path.join PurePath.joinpath
os.path.basename PurePath.name
os.path.dirname PurePath.parent
os.path.samefile Path.samefile
os.path.splitext PurePath.suffix
小结

pathlib是一个比os.path更优秀的库,它以面向对象的编程方式来表示和处理文件系统路径。它支持直接使用"/"拼接路径,并支持链式查询,代码比os.path更简洁。Python 3.6和Django 3.1后建议使用pathlib库处理文件系统路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值