自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

還在使用 os.path?Python 中的 Pathlib 太香了

開發(fā) 前端
相信現(xiàn)在依然有很多人習(xí)慣于使用os來(lái)處理文件/文件夾/路徑等,但其實(shí)Python自帶的Pathlib庫(kù)處理這些更優(yōu)雅、更安全。

寫在前面

相信現(xiàn)在依然有很多人習(xí)慣于使用os來(lái)處理文件/文件夾/路徑等,但其實(shí)Python自帶的Pathlib庫(kù)處理這些更優(yōu)雅、更安全,你會(huì)發(fā)現(xiàn)很多大型開源項(xiàng)目都在使用它,那么它到底有什么魅力?花點(diǎn)時(shí)間讓我們一起看看吧!

先看這個(gè)引例,假設(shè)我們需要獲取某個(gè)文件夾下所有的txt文件,基于os會(huì)寫成如下形式:

import os

dir_path = "/home/user/documents"# Find all text files inside a directory
files = [os.path.join(dir_path, f) for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f)) and f.endswith(".txt")]

使用Pathlib則變成如下形式,是不是心動(dòng)了:

from pathlib import Path

dir_path = Path("/home/user/documents")
files = list(dir_path.glob("*.txt"))

os.path 的最大缺點(diǎn)是將系統(tǒng)路徑視為字符串,極容易導(dǎo)致混亂,Pathlib 在Python3.4中被支持, 通過(guò)將路徑表示為獨(dú)特的對(duì)象解決了這個(gè)問(wèn)題,并為路徑處理引入更多可擴(kuò)展用法,許多操作在os需要層層嵌套,而Pathlib將使開發(fā)人員更輕松地處理與路徑和文件相關(guān)的所有事情。

處理路徑

1.創(chuàng)建路徑

幾乎所有pathlib 的功能都可以通過(guò)其 Path 子類訪問(wèn),可以使用該類創(chuàng)建文件和目錄,有多種初始化Path的方式,比如,使用當(dāng)前工作路徑:

from pathlib import Path

Path.cwd() # PosixPath('/home/user/Downloads')

使用home:

Path.home() # PosixPath('/home/user')

同樣的可以指定字符串路徑創(chuàng)建路徑:

p = Path("documents") # PosixPath('documents')

使用正斜杠運(yùn)算符進(jìn)行路徑連接:

data_dir = Path(".") / "data"
csv_file = data_dir / "file.csv"
print(data_dir) # data
print(csv_file) # data/file.csv

檢查路徑是否存在,可以使用布爾函數(shù) exists:

data_dir.exists() 

data_dir.exists() 檢查文件是否存在:

csv_file.exists()

csv_file.exists() 使用 is_dir 或 is_file 函數(shù)來(lái)檢查是否為文件夾、文件:

data_dir.is_dir()
csv_file.is_file()

大多數(shù)路徑都與當(dāng)前運(yùn)行目錄相關(guān),但某些情況下必須提供文件或目錄的絕對(duì)路徑,可以使用 absolute

csv_file.absolute() # PosixPath('/home/user/Downloads/data/file.csv')

如果仍然需要將路徑轉(zhuǎn)為字符串,可以調(diào)用 str(path) 強(qiáng)制轉(zhuǎn)換:

str(Path.home()) # '/home/user'

現(xiàn)如今大多數(shù)庫(kù)都支持 Path 對(duì)象,包括 sklearn 、 pandas 、 matplotlib 、 seaborn 等。

2.Path屬性

Path 對(duì)象有許多有用屬性,一起來(lái)看看這些示例,首先定義一個(gè)圖片路徑:

image_file = Path("images/shadousheng.png").absolute() # PosixPath('/home/user/Downloads/images/midjourney.png')

先從 parent 開始,它將返回當(dāng)前工作目錄的上一級(jí):

image_file.parent # PosixPath('/home/user/Downloads/images')

獲取文件名:

image_file.name # 'shadousheng.png'

它將返回帶有后綴的文件名,若只想要前綴,則使用stem:

image_file.stem # shadousheng

只想要后綴也很簡(jiǎn)單:

image_file.suffix # '.png'

image_file.suffix # '.png' 如果要將路徑分成多個(gè)部分,可以使用 parts:

image_file.parts # ('/', 'home', 'user', 'Downloads', 'images', 'shadousheng.png')

如果希望這些組件本身就是 Path 對(duì)象,可以使用 parents 屬性,它會(huì)創(chuàng)建一個(gè)生成器

for i in image_file.parents:
    print(i)

# /home/user/Downloads/images
# /home/user/Downloads
# /home/user
# /home
# /

3.處理文件

想要?jiǎng)?chuàng)建文件并寫入內(nèi)容,不必再使用 open 函數(shù),只需創(chuàng)建一個(gè) Path 對(duì)象搭配 write_text 或 write_btyes 即可:

markdown = data_dir / "file.md"

# Create (override) and write text
markdown.write_text("# This is a test markdown")

讀取文件,可以 read_text 或 read_bytes:

markdown.read_text() # '# This is a test markdown'
len(image_file.read_bytes()) # 1962148

但請(qǐng)注意, write_text 或 write_bytes 會(huì)覆蓋文件的現(xiàn)有內(nèi)容:

# Write new text to existing file
markdown.write_text("## This is a new line")
# The file is overridden
markdown.read_text() # '## This is a new line'

要將新信息附加到現(xiàn)有文件,應(yīng)該在 a (附加)模式下使用 Path 對(duì)象的 open 方法:

# Append text
with markdown.open(mode="a") as file:
    file.write("\n### This is the second line")

markdown.read_text() # '## This is a new line\n### This is the second line'

使用rename 重命名文件,如在當(dāng)前目錄中重命名,file.md 變成了 new_markdown.md:

renamed_md = markdown.with_stem("new_markdown")

markdown.rename(renamed_md) # PosixPath('data/new_markdown.md')

通過(guò) stat().st_size 查看文件大小:

# Display file size
renamed_md.stat().st_size # 49

查看最后一次修改文件的時(shí)間:

from datetime import datetime

modified_timestamp = renamed_md.stat().st_mtime

datetime.fromtimestamp(modified_timestamp) # datetime.datetime(2023, 8, 1, 13, 32, 45, 542693)

st_mtime 返回一個(gè)自 1970 年 1 月 1 日以來(lái)的秒數(shù)。為了使其可讀,搭配使用 datatime 的 fromtimestamp 函數(shù)。

要?jiǎng)h除不需要的文件,可以 unlink:

renamed_md.unlink(missing_ok=True)

如果文件不存在,將 missing_ok 設(shè)置為 True 則不會(huì)引起報(bào)錯(cuò)

4.處理目錄

首先,看看如何遞歸創(chuàng)建目錄:

new_dir.mkdir(parents=True, exist_ok=True)

默認(rèn)情況下, mkdir 創(chuàng)建給定路徑的最后一個(gè)子目錄,如果中間父級(jí)不存在,則必須將 parents 設(shè)置為 True 達(dá)到遞歸創(chuàng)建目的。

要?jiǎng)h除目錄,可以使用 rmdir ,如果給定的路徑對(duì)象是嵌套的,則僅刪除最后一個(gè)子目錄:

new_dir.rmdir()

要在終端上列出 ls 等目錄的內(nèi)容,可以使用 iterdir 。結(jié)果將是一個(gè)生成器對(duì)象,一次生成一個(gè)子內(nèi)容作為單獨(dú)的路徑對(duì)象,和os.listdir不同的是,它返回每個(gè)內(nèi)容的絕對(duì)路徑而不是名字:

for p in Path.home().iterdir():
    print(p)
# /home/user/anaconda3
# /home/user/.googleearth
# /home/user/.zcompdump
# /home/user/.ipython
# /home/user/.bashrc

要捕獲具有特定擴(kuò)展名或名稱的所有文件,可以將 glob 函數(shù)與正則表達(dá)式結(jié)合使用。

例如,使用 glob("*.txt") 查找主目錄中所有文本文件:

home = Path.home()
text_files = list(home.glob("*.txt"))

len(text_files) # 3

要遞歸搜索文本文件(即在所有子目錄中),可以glob 與 rglob 結(jié)合使用:

all_text_files = [p for p in home.rglob("*.txt")]

len(all_text_files) # 5116

以上就是Pathlib中常用方法。

責(zé)任編輯:趙寧寧 來(lái)源: 啥都會(huì)一點(diǎn)的研究生
相關(guān)推薦

2021-05-11 07:10:18

標(biāo)準(zhǔn)庫(kù)DjangoOS

2010-03-25 12:50:45

Python代碼

2021-09-24 09:30:05

os.path模塊Python

2024-04-29 08:32:21

os.path模塊Python內(nèi)置函數(shù)

2023-11-07 10:36:37

2020-12-21 07:36:15

緩存數(shù)據(jù)庫(kù)緩存層

2024-01-05 13:26:00

KafkaTopicSpring

2024-01-26 07:48:10

SpringKafka提升

2020-10-10 11:07:38

Java開發(fā)代碼

2021-07-28 14:20:13

正則PythonFlashText

2020-12-28 11:09:40

Python正則表達(dá)式代碼

2023-11-09 08:01:41

Spring緩存注解

2025-01-09 11:24:59

線程池美團(tuán)動(dòng)態(tài)配置中心

2024-08-09 08:55:43

if執(zhí)行器版本

2021-01-11 08:03:30

阿里中臺(tái)項(xiàng)目

2021-04-26 07:31:22

SpringMVCweb框架

2021-08-03 05:22:49

微信借條騰訊

2021-12-15 10:01:06

Python進(jìn)度條開發(fā)

2020-12-02 16:40:00

微信新功能移動(dòng)應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)