Python 标准库之pathlib,路径操作

背景

pathlib 标准库是在 Python3.4 引入,到现在最近版 3.11 已更新了好几个版本,主要是用于路径操作,相比之前的路径操作方法 os.path 有一些优势,有兴趣的同学可以学习下

**官方文档:**https://docs.python.org/zh-cn/3/library/pathlib.html
官方pathlib图片

小编环境

import sys

print('python 版本:',sys.version.split('|')[0])   #python 版本: 3.11.4

主要方法、函数

该模块中主要使用的是 Path

  • 导入模块
from pathlib import Path
  • 获取当前工作目录
Path.cwd()   #WindowsPath('D:/桌面/Python/标准库')
  • 获取用户 home 目录
Path.home()   #WindowsPath('C:/Users/admin')
  • 获取绝对路径
file = Path('pathlib_demo1.py')
print(file)   #WindowsPath('pathlib_demo1.py')
file.resolve()  #WindowsPath('D:/桌面/Python/标准库/pathlib_demo1.py')
  • 获取文件属性
file = Path('pathlib_demo1.py')
print(file)   #WindowsPath('pathlib_demo1.py')

file.stat()  
'''
os.stat_result(st_mode=33206, st_ino=1970324837176895, st_dev=2522074357, 
st_nlink=1, st_uid=0, st_gid=0, st_size=273, 
st_atime=1695642854, st_mtime=1695611301, st_ctime=1695611241)
'''

#文件大小
file.stat().st_size   #273B

#最近访问时间 access ,It represents the time of most recent access 
file.stat().st_atime  #1695625134.9083948

#创建时间 create,It represents the time of most recent metadata change on Unix and creation time on Windows.
file.stat().st_ctime  #1695611241.5981772

#修改时间  modify,It represents the time of most recent content modification
file.stat().st_mtime  #1695611301.1193473
  • 查看当前工作目录文件及文件夹
for f in path.iterdir():
    print(f)
    print('is_file:',f.is_file())  #判断是否为文件
    print('is_dir:',f.is_dir())   #判断是否为文件夹
    print('='*30)

'''
D:\桌面\Python\标准库\.ipynb_checkpoints
is_file: False
is_dir: True
==============================
D:\桌面\Python\标准库\pathlib.ipynb
is_file: True
is_dir: False
==============================
D:\桌面\Python\标准库\pathlib_demo1.py
is_file: True
is_dir: False
==============================
'''
  • 路径的各个组成部分
file=Path('D:\桌面\Python\标准库\pathlib_demo1.py')

file.name  #'pathlib_demo1.py'
file.stem  #'pathlib_demo1'
file.suffix   #'.py'
file.parent   #WindowsPath('D:/桌面/Python/标准库')
file.anchor  #'D:\\'
file.parent.parent  #WindowsPath('D:/桌面/Python')

#获取所有的父级路径,层层递进
list(file.parents)
'''
[WindowsPath('D:/桌面/Python/标准库'),
 WindowsPath('D:/桌面/Python'),
 WindowsPath('D:/桌面'),
 WindowsPath('D:/')]
'''
  • 路径拼接
    支持2种方式
#第1种方式:使用 /
Path.home() / 'dir' / 'file.txt'  #WindowsPath('C:/Users/admin/dir/file.txt')

#第2种方式:使用方法
Path.home().joinpath('dir', 'file.txt')  #WindowsPath('C:/Users/admin/dir/file.txt')
  • 判断路径、文件是否存在
#当前文件件里面是否存在 子目录 archive/demo.txt 文件
Path("archive/demo.txt").exists()  #False

#当前文件件里面是否存在 二级子目录 dir/subdir  
Path('dir/subdir').exists()   #True

#当前文件件里面是否存在 pathlib_demo1.py 文件
Path("pathlib_demo1.py").exists()  #True

以上是自己实践中遇到的一些问题,分享出来供大家参考学习,欢迎关注微信公众号:DataShare ,不定期分享干货

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据人阿多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值