![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5SM2czN5MTYkljN2MGOwgDNyYzX2QjNyYTMyAzLcRDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
相信你一定用os庫對檔案系統進行過操作,比如檔案讀寫,路徑組合,上傳下載下傳等都會涉及到檔案路徑。但是某些操作使用os庫就很不優雅,例如查找上級路徑,不同作業系統間的路徑處理等。今天我們就介紹一個Python内置的面向對象的路徑庫pathlib。
1、擷取目前檔案所在路徑
from os import path
exec_path = path.dirname(__file__)
print(exec_path,type(exec_path))
from pathlib import Path,PurePath
cur_path = Path(__file__).parent
print(cur_path,type(cur_path))
運作結果:
e:/TestProject/iaas/test_data <class 'str'>
e:\TestProject\iaas\test_data <class 'pathlib.WindowsPath'>
總結:
- os庫傳回的路徑是str對象
- 而pathlib傳回的路徑是一個WindowsPath的對象
- WindowsPath對象有很多路徑的屬性,而str隻有str的屬性
2、擷取目前檔案的名稱
from os import path
exec_path = path.basename(__file__)
print(exec_path,type(exec_path))
from pathlib import Path,PurePath
cur_path = Path(__file__).name
print(cur_path,type(cur_path))
運作結果:
t_test.py <class 'str'>
t_test.py <class 'str'>
總結:
- os的庫擷取目前檔案名稱需要使用basename方法實作
- pathlib擷取目前檔案名稱隻需要擷取name屬性的值即可
3、查找父路徑
from os import path,getcwd
'''
cur_path:目前路徑
par_path:父路徑
gra_path:父父路徑
'''
cur_path = path.dirname(__file__)
par_path = path.dirname(path.dirname(__file__))
gra_path = path.dirname(path.dirname(path.dirname(__file__)))
print(cur_path)
print(par_path)
print(gra_path)
from pathlib import Path,PurePath
cur_path = Path(__file__).parent
par_path = cur_path.parent
gra_path = par_path.parent
print(cur_path)
print(par_path)
print(gra_path)
運作結果:
e:/TestProject/iaas/test_data
e:/TestProject/iaas
e:/TestProject
e:\TestProject\iaas\test_data
e:\TestProject\iaas
e:\TestProject
總結:pathlib的方式要比os的方式優雅甚多,突出展現了面向對象的優勢。
4、pathlib的PurePath和Path
- PurePath代表純路徑,不代表真正的路徑和檔案,純路徑對象操作
- Path代表真正的路徑和檔案,可以判斷路徑或者檔案的屬性和是否存在
- Path是PurePath的子類,支援PurePath的各種屬性和方法
- PurePath有兩個子類,PureWindowsPath和PurePosixPath,分别代表不同的作業系統的純路徑對象
Windows:
from pathlib import Path,PurePath
cur_path = Path(__file__)
print(cur_path,type(cur_path))
pur_path = PurePath(__file__)
print(pur_path,type(pur_path))
運作結果:
e:\TestProject\iaas\test_data\t_test.py <class 'pathlib.WindowsPath'>
e:\TestProject\iaas\test_data\t_test.py <class 'pathlib.PureWindowsPath'>
Linux:
>>> from pathlib import PurePath
>>> from pathlib import Path
>>> cur_path = Path('/home/env/internet')
>>> print(cur_path,type(cur_path))
/home/env/internet <class 'pathlib.PosixPath'>
>>> pur_path = PurePath('/home/env/internet')
>>> print(pur_path,type(pur_path))
/home/env/internet <class 'pathlib.PurePosixPath'>
總結:使用PurePath就可以實作跨作業系統的路徑統一操作。
5、pathlib和os的功能對照
6、常用的pathlib.Path相關方法
Path.iterdir() # 周遊目錄的子目錄或者檔案
Path.is_dir() # 判斷是否是目錄
Path.glob() # 過濾目錄(傳回生成器)
Path.resolve() # 傳回絕對路徑
Path.exists() # 判斷路徑是否存在
Path.open() # 打開檔案(支援with)
Path.unlink() # 删除檔案或目錄(目錄非空觸發異常)
Path.parts # 分割路徑 類似os.path.split(), 不過傳回元組
Path.drive # 傳回驅動器名稱
Path.root # 傳回路徑的根目錄
Path.anchor # 自動判斷傳回drive或root
Path.parents # 傳回所有上級目錄的清單
Path.with_name() # 更改路徑名稱, 更改最後一級路徑名
Path.with_suffix() # 更改路徑字尾
Path.joinpath() # 拼接路徑
Path.relative_to() # 計算相對路徑
Path.match() # 測試路徑是否符合pattern
Path.is_dir() # 是否是檔案
Path.is_absolute() # 是否是絕對路徑
Path.is_reserved() # 是否是預留路徑
Path.exists() # 判斷路徑是否真實存在
Path.cwd() # 傳回目前目錄的路徑對象
Path.home() # 傳回目前使用者的home路徑對象
Path.stat() # 傳回路徑資訊, 同os.stat()
Path.chmod() # 更改路徑權限, 類似os.chmod()
Path.expanduser() # 展開~傳回完整路徑對象
Path.mkdir() # 建立目錄
Path.rename() # 重命名路徑
Path.rglob() # 遞歸周遊所有子目錄的檔案