599 字
3 分钟
Python 标准库之pathlib,路径操作
背景
pathlib 标准库是在 Python3.4 引入,到现在最近版 3.11 已更新了好几个版本,主要是用于路径操作,相比之前的路径操作方法 os.path 有一些优势,有兴趣的同学可以学习下
小编环境
import sys
print('python 版本:',sys.version.split('|')[0]) #python 版本: 3.11.4主要方法、函数
该模块中主要使用的是 Path 类
from pathlib import PathPath.cwd() #WindowsPath('D:/桌面/Python/标准库')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 accessfile.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 modificationfile.stat().st_mtime #1695611301.1193473for f in path.iterdir(): print(f) print('is_file:',f.is_file()) #判断是否为文件 print('is_dir:',f.is_dir()) #判断是否为文件夹 print('='*30)
'''D:\桌面\Python\标准库\.ipynb_checkpointsis_file: Falseis_dir: True==============================D:\桌面\Python\标准库\pathlib.ipynbis_file: Trueis_dir: False==============================D:\桌面\Python\标准库\pathlib_demo1.pyis_file: Trueis_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/subdirPath('dir/subdir').exists() #True
#当前文件件里面是否存在 pathlib_demo1.py 文件Path("pathlib_demo1.py").exists() #True历史相关文章
以上是自己实践中遇到的一些问题,分享出来供大家参考学习,欢迎关注微信公众号:DataShare ,不定期分享干货
