python中pathlib 面向?qū)ο蟮奈募到y(tǒng)路徑
pathlib:面向?qū)ο蟮奈募到y(tǒng)路徑
pathlib官方介紹: Python3.4+內(nèi)置的標(biāo)準(zhǔn)庫(kù),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í)的庫(kù)。
3、os.path操作的路徑時(shí)字符串,而pathlib操作的路徑時(shí)對(duì)象。提供了對(duì)資源路徑和資源命名結(jié)構(gòu)的通用抽象。它把文件系統(tǒng)接口從os模塊中隔離出來(lái),打開(kāi)了從應(yīng)用層配置文件系統(tǒng)的大門。
換句話說(shuō),pathlib.Path這個(gè)接口代表的可以不僅是os的資源路徑,還可以是HDFS的資源路徑,RESTful API的資源路徑。
3. 基本介紹
1、官網(wǎng)上給出的該模塊提供表示文件系統(tǒng)路徑的類,如下,其語(yǔ)義適用于不同的操作系統(tǒng),路徑類被分為提供純計(jì)算操作而沒(méi)有 I/O 的純路徑,以及從純路徑繼承而來(lái)但提供 I/O 操作的具體路徑。一般情況下,直接使用Path即可。

2、Path屬性

4. 常見(jiàn)用法
獲取當(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)文章
關(guān)于keras.layers.Conv1D的kernel_size參數(shù)使用介紹
這篇文章主要介紹了關(guān)于keras.layers.Conv1D的kernel_size參數(shù)使用介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
解決Pandas to_json()中文亂碼,轉(zhuǎn)化為json數(shù)組的問(wèn)題
今天小編就為大家分享一篇解決Pandas to_json() 中文亂碼,轉(zhuǎn)化為json數(shù)組的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Python實(shí)例之wxpython中Frame使用方法
本文介紹下wxpython中Frame的用法,不錯(cuò)的python編程實(shí)例,有需要的朋友參考下2014-06-06
Python中的進(jìn)程操作模塊(multiprocess.process)
這篇文章介紹了Python中的進(jìn)程操作模塊(multiprocess.process),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
python 實(shí)現(xiàn)圍棋游戲(純tkinter gui)
這篇文章主要介紹了python 如何實(shí)現(xiàn)圍棋游戲,幫助大家利用tkinter制作圖形界面程序,感興趣的朋友可以了解下2020-11-11
react中useLayoutEffect 和useEffect區(qū)別
pytorch 實(shí)現(xiàn)凍結(jié)部分參數(shù)訓(xùn)練另一部分

