對(duì)python3中pathlib庫(kù)的Path類的使用詳解
用了很久的os.path,今天發(fā)現(xiàn)竟然還有這么好用的庫(kù),記錄下來(lái)以便使用。
1.調(diào)用庫(kù)
from pathlib import
2.創(chuàng)建Path對(duì)象
p = Path('D:/python/1.py') print(p) #可以這么使用,相當(dāng)于os.path.join() p1 = Path('D:/python') p2 = p1/'123' print(p2)
結(jié)果
D:\python\1.py D:\python\123
3.Path.cwd()
獲取當(dāng)前路徑
path = Path.cwd() print(path)
結(jié)果:
D:\python
4.Path.stat()
獲取當(dāng)前文件的信息
p = Path('1.py') print(p.stat())
結(jié)果
os.stat_result(st_mode=33206, st_ino=8444249301448143, st_dev=2561774433, st_nlink=1, st_uid=0, st_gid=0, st_size=4, st_atime=1525926554, st_mtime=1525926554, st_ctime=1525926554)
5.Path.exists()
判斷當(dāng)前路徑是否是文件或者文件夾
>>> Path('.').exists() True >>> Path('1.py').exists() True >>> Path('2.py').exists() False
6.Path.glob(pattern)與Path.rglob(pattern)
Path.glob(pattern):獲取路徑下的所有符合pattern的文件,返回一個(gè)generator
目錄下的文件如下:
以下是獲取該目錄下所有py文件的路徑:
path=Path.cwd() pys = path.glob('*.py')#pys是經(jīng)過(guò)yield產(chǎn)生的迭代器 for py in pys: print(py)
結(jié)果:
C:\python\1.py C:\python\11.py C:\python\1111.py C:\python\11111.py
Path.rglob(pattern):與上面類似,只不過(guò)是返回路徑中所有子文件夾的符合pattern的文件。
7.Path.is_dir()與Path.is_file()
Path.is_dir()判斷該路徑是否是文件夾 Path.is_file()判斷該路徑是否是文件 print('p1:') p1 = Path('D:/python') print(p1.is_dir()) print(p1.is_file()) print('p2:') p2 = Path('D:/python/1.py') print(p2.is_dir()) print(p2.is_file()) #當(dāng)路徑不存在時(shí)也會(huì)返回Fasle print('wrong path:') print(Path('D:/NoneExistsPath').is_dir()) print(Path('D:/NoneExistsPath').is_file())
結(jié)果
p1: True False p2: False True wrong path: False False
8.Path.iterdir()
當(dāng)path為文件夾時(shí),通過(guò)yield產(chǎn)生path文件夾下的所有文件、文件夾路徑的迭代器
p = Path.cwd() for i in p.iterdir(): print(i)
結(jié)果
D:\python\1.py D:\python\11.py D:\python\1111.py D:\python\11111.py D:\python\dir
9.Path.mkdir(mode=0o777,parents=Fasle)
根據(jù)路徑創(chuàng)建文件夾
parents=True時(shí),會(huì)依次創(chuàng)建路徑中間缺少的文件夾
p_new = p/'new_dir' p_new.mkdir() p_news = p/'new_dirs/new_dir' p_news.mkdir(parents=True)
結(jié)果
10.Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
類似于open()函數(shù)
11.Path.rename(target)
當(dāng)target是string時(shí),重命名文件或文件夾
當(dāng)target是Path時(shí),重命名并移動(dòng)文件或文件夾
p1 = Path('1.py') p1.rename('new_name.py') p2 = Path('11.py') target = Path('new_dir/new_name.py') p2.rename(target)
結(jié)果
12.Path.replace(target)
重命名當(dāng)前文件或文件夾,如果target所指示的文件或文件夾已存在,則覆蓋原文件
13.Path.parent(),Path.parents()
parent獲取path的上級(jí)路徑,parents獲取path的所有上級(jí)路徑
14.Path.is_absolute()
判斷path是否是絕對(duì)路徑
15.Path.match(pattern)
判斷path是否滿足pattern
16.Path.rmdir()
當(dāng)path為空文件夾的時(shí)候,刪除該文件夾
17.Path.name
獲取path文件名
18.Path.suffix
獲取path文件后綴
以上這篇對(duì)python3中pathlib庫(kù)的Path類的使用詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Python import .pyd 可能遇到路徑的問(wèn)題
這篇文章主要介紹了解決Python import .pyd 可能遇到路徑的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03python Hypothesis生成和執(zhí)行大量的測(cè)試用例
Hypothesis是一個(gè)基于屬性的測(cè)試(property-based testing)庫(kù),它能夠幫助我們生成和執(zhí)行大量的測(cè)試用例,與傳統(tǒng)的單元測(cè)試相比,屬性測(cè)試更加靈活和全面,能夠發(fā)現(xiàn)更多的邊界情況和潛在的錯(cuò)誤2024-01-01python3調(diào)用ansible?api使用實(shí)例例說(shuō)明
這篇文章主要為大家介紹了python3?調(diào)用ansible?api使用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07使用matplotlib庫(kù)實(shí)現(xiàn)圖形局部數(shù)據(jù)放大顯示的實(shí)踐
本文主要介紹了使用matplotlib庫(kù)實(shí)現(xiàn)圖形局部數(shù)據(jù)放大顯示的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Python中的變量及簡(jiǎn)單數(shù)據(jù)類型應(yīng)用
這篇文章主要介紹了Python中的變量及簡(jiǎn)單數(shù)據(jù)類型應(yīng)用,簡(jiǎn)單的數(shù)據(jù)類型包括字符串和數(shù)字,更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-03-03Opencv實(shí)現(xiàn)二維直方圖的計(jì)算及繪制
這篇博客將介紹如何使用Opencv進(jìn)行二維直方圖的計(jì)算及繪制,維直方圖可以讓我們對(duì)不同的像素密度有更好的了解,感興趣的可以了解一下2021-07-07