美文网首页
Python 标准库之pathlib,路径操作

Python 标准库之pathlib,路径操作

作者: 数据人阿多 | 来源:发表于2023-09-25 20:21 被阅读0次

背景

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 ,不定期分享干货

相关文章

  • Python 一些有趣的技巧,包括协程例

    1. 路径操作比起 os 模块的 path 方法,python3 标准库的 pathlib 模块的 Path 处理...

  • python标准库pathlib常见操作

    pathlib—— 面向对象的文件系统路径它是python3.4新增的一个标准库,提供了不同操作系统下文件系统路径...

  • 路径处理库pathlib使用记录

    Python3的系统标准库pathlib模块的 Path 对路径的操作会更简单,基本可以替代 os.path。新版...

  • pathlib

    一、说明 pathlib 是在Python3上使用的强悍路径管理库,以简洁的方式管理各种操作系统中使用的路径。 二...

  • 对pathlib进行扩展

    pathlib 自 Python 3.4 以后成为了 Python 的标准库,该库非常的好用,大大简化了目录的管理...

  • python 路径处理

    python 路径处理主要用到的两个库 os.path and pathlib requirements pyth...

  • Python标准库(1)—Pathlib

    准备开一个长期专题,将学习《Python标准库》中的一些demo记录下来,方便查询和回忆。Python的版本是3....

  • Python标准库系列之pathlib模块

    首先我们看使用OS模块连接目录和文件 此代码可以在各个平台顺利运行,但是反复使用os.path.join很啰嗦,重...

  • python 路径管理 sys, os, Pathlib

    python 获得文件所在绝对路径 获取文件当前工作目录路径(绝对路径) 获取上级路径 python Pathlib

  • Python3 使用pathlib替代os.path

    路径管理库pathlib在python 3.4版本之后支持,它可以让我们采用面向对象的方式来处理路径,带来十分轻快...

网友评论

      本文标题:Python 标准库之pathlib,路径操作

      本文链接:https://www.haomeiwen.com/subject/vyjcbdtx.html