python中pathlib 面向?qū)ο蟮奈募到y(tǒng)路徑
pathlib:面向?qū)ο蟮奈募到y(tǒng)路徑
pathlib官方介紹: Python3.4+內(nèi)置的標(biāo)準(zhǔn)庫,Object-oriented filesystem paths(面向?qū)ο蟮奈募到y(tǒng)路徑)
1. 使用示例
1.1 最常用:獲取項(xiàng)目目錄
os.path方法
import os project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) print(project_dir) # /Users/root/Code/project print(type(project_dir)) # <class 'str'>
pathlib方法
from pathlib import Path project_dir = Path(__file__).resolve().parent.parent print(project_dir) # /Users/root/Code/project print(type(project_dir)) # <class 'pathlib.PosixPath'>
1.2 遍歷一個(gè)目錄下指定文件
os方法
file_list = [] for file in os.listdir(csv_dir): if not file.endswith('.csv'): continue file_list.append(file)
pathlib方法
from pathlib import Path csv_dir = Path(csv_dir) file_list = csv_dir.glob('*.csv')
1.3 遞歸目錄下的所有文件
os方法
os.walk方法
file_list = [] for root, dirs, files in os.walk('/Users/root/Code/project'): for f in files: if f.endswith('.py'): file_list.append(os.path.join(root, f))
# glob方法 from glob import glob file_list = glob('/Users/root/Code/project/**/*.py', recursive=True)
pathlib方法
file_list = csv_dir.rglob('*.csv') # 生成器對(duì)象
2. 優(yōu)勢(shì)對(duì)比
1、當(dāng)需要獲取多個(gè)層級(jí)目錄時(shí),os.path需要使用嵌套的寫法,而pathlib則是正常的鏈?zhǔn)秸{(diào)用。pathlib的使用更加友好,使得路徑對(duì)象更加明確。
2、os.path只用于基本的路徑處理,如果需要其他操作如重命名/新建/復(fù)制文件等,需要用os下的其他模塊。而pathlib可以用于處理路徑相關(guān)操作,并且是一個(gè)輕量級(jí)的庫。
3、os.path操作的路徑時(shí)字符串,而pathlib操作的路徑時(shí)對(duì)象。提供了對(duì)資源路徑和資源命名結(jié)構(gòu)的通用抽象。它把文件系統(tǒng)接口從os模塊中隔離出來,打開了從應(yīng)用層配置文件系統(tǒng)的大門。
換句話說,pathlib.Path這個(gè)接口代表的可以不僅是os的資源路徑,還可以是HDFS的資源路徑,RESTful API的資源路徑。
3. 基本介紹
1、官網(wǎng)上給出的該模塊提供表示文件系統(tǒng)路徑的類,如下,其語義適用于不同的操作系統(tǒng),路徑類被分為提供純計(jì)算操作而沒有 I/O 的純路徑,以及從純路徑繼承而來但提供 I/O 操作的具體路徑。一般情況下,直接使用Path即可。
2、Path屬性
4. 常見用法
獲取當(dāng)前文件絕對(duì)路徑
from pathlib import Path path = Path(__file__).resolve() path >>> /Users/root/Code/project/conf/path_conf.py
獲取當(dāng)前文件的上級(jí)目錄
from pathlib import Path path = Path(__file__).resolve() path.parent >>> /Users/root/Code/project/conf path.parent.parent >>> /Users/root/Code/project
獲取文件名
from pathlib import Path path = Path(__file__).resolve() path.name >>> path_conf.py path.stem >>> path_conf path.suffix >>> .py
修改文件名,修改后綴
from pathlib import Path file = Path('data.csv') new_file = file.with_name('result.csv') new_file = file.with_suffix('.txt')
路徑拼接
from pathlib import Path root = Path(__file__).resolve().parent # 方式1 conf_file = root / 'conf/path_conf.py' # 方式2 conf_file = root / Path('conf/path_conf.py') # 方式3 conf_file = root.joinpath('conf/path_conf.py')
路徑判斷
from pathlib import Path path = Path(__file__).resolve() path.is_file() # 是否為文件 path.is_dir() # 是否為目錄 path.exists() # 是否存在
文件操作
from pathlib import Path file = Path(__file__).resolve() file.touch() # 新建文件 file.replace('../data.csv') # 移動(dòng)文件 file.unlink() # 刪除文件
讀取文件
file = Path('../data/1.csv') data = file.read_text() # 讀取文件 data = file.write_text() # 寫入文件 # 也可以作為普通路徑使用 with open(file, 'r') as f: data = f.read()
5. 參考資料
https://docs.python.org/zh-cn/3/library/pathlib.html
https://www.cnblogs.com/heniu/p/12872604.html
https://zhuanlan.zhihu.com/p/87940289
https://zhuanlan.zhihu.com/p/33524938
https://www.jianshu.com/p/a820038e65c3
https://cloud.tencent.com/developer/article/1640696
相關(guān)文章
對(duì)python中矩陣相加函數(shù)sum()的使用詳解
今天小編就為大家分享一篇對(duì)python中矩陣相加函數(shù)sum()的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01python目標(biāo)檢測(cè)SSD算法訓(xùn)練部分源碼詳解
這篇文章主要為大家介紹了python目標(biāo)檢測(cè)SSD算法訓(xùn)練部分源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05