最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Python 目錄和文件操作 - pathlib

2023-02-19 15:02 作者:梨幾OvO  | 我要投稿

目錄和文件操作 - pathlib

面向?qū)ο蟮哪夸?、文件系統(tǒng)模塊,可以取代os.path

導(dǎo)入Path類

from?pathlib?import?Path

遍歷子目錄 - path.iterdir()

p?=?Path(r'E:\WAV?Sound')

for?i?in?p.iterdir():
????print(i)
E:\WAV?Sound\cymatics.fm
E:\WAV?Sound\Dannasko?Anime?Vocal?Samples
E:\WAV?Sound\Sounds?of?Himalayas?Sample?Pack?Vol.1[Future?Bass?Edition]

使用通配符深度遍歷 - path.glob(pattern), path.rglob(pattern)

count?=?0?#?控制展示結(jié)果數(shù)量
for?i?in?p.glob('**/*.wav'):
????if?count?>10:?break
????print(i)
????count?+=1
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Classic?Melody?Loop?1?-?90?BPM?D?Maj.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Classic?Melody?Loop?2?-?155?BPM?D#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Classic?Melody?Loop?3?-?160?BPM?A?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Classic?Melody?Loop?4?-?165?BPM?D#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?1?-?83?BPM?C?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?10?-?170?BPM?F#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?11?-?170?BPM?F#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?12?-?170?BPM?B?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?2?-?100?BPM?G#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?3?-?126?BPM?B?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?4?-?128?BPM?C?Min.wav

** 表示遞歸匹配,python3.9之后可以用 rglob(\'*.wav\') 代替 glob(\'**/*.wav\'),效果相同

count?=?0?#?控制展示結(jié)果數(shù)量
for?i?in?p.rglob('*.wav'):
????if?count?>10:?break
????print(i)
????count?+=1
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Classic?Melody?Loop?1?-?90?BPM?D?Maj.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Classic?Melody?Loop?2?-?155?BPM?D#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Classic?Melody?Loop?3?-?160?BPM?A?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Classic?Melody?Loop?4?-?165?BPM?D#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?1?-?83?BPM?C?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?10?-?170?BPM?F#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?11?-?170?BPM?F#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?12?-?170?BPM?B?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?2?-?100?BPM?G#?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?3?-?126?BPM?B?Min.wav
E:\WAV?Sound\cymatics.fm\Cymatics?-?2020?Melody?Collection\Cymatics?-?2020?Dark?Melody?Loop?4?-?128?BPM?C?Min.wav

判斷路徑屬性:

p.exists()?#?路徑是否存在
True
p.is_file()?#?是否是文件
False
p.is_dir()?#?是否是目錄
True

路徑拼接:

file_path?=?p?/?'test_file.txt'
file_path.exists()
False

創(chuàng)建文件:

file_path.touch()?#?只要不報(bào)錯(cuò),就是創(chuàng)建成功
file_path.exists()?and?file_path.is_file()
True

文件讀寫:

#?和python?內(nèi)置函數(shù)?open()類似
with?file_path.open('w',encoding='utf-8')?as?f:
????f.write('Hello!')

#?寫入字符串更簡(jiǎn)單的用法,文件會(huì)自動(dòng)關(guān)閉
file_path.write_text('Hello',?encoding='utf-8')

#?讀取同理
with?file_path.open('r',encoding='utf-8')?as?f:
????f.read()

file_path.read_text(encoding='utf-8')

#?二進(jìn)制讀寫可用?write_bytes(data),?read_bytes()
'Hello'

文件重命名和刪除:

用with_stem返回一個(gè)新路徑,作為重命名的目標(biāo)參數(shù),也可以with_name(), with_suffix()

new_path?=?file_path.with_stem('test_file_1')
new_path_2?=?file_path.with_name('test_file_2.txt')
new_json_path?=?file_path.with_suffix('.json')

print(new_path,?new_path.exists())
print(new_path_2,?new_path_2.exists())
print(new_json_path,?new_json_path.exists())
E:\WAV?Sound\test_file_1.txt?False
E:\WAV?Sound\test_file_2.txt?False
E:\WAV?Sound\test_file.json?False

重命名為 new_path,并把返回的路徑對(duì)象賦值給file_path

file_path?=?file_path.rename(new_path)

print(new_path.exists())
print(file_path,?file_path.exists())
True
E:\WAV?Sound\test_file_1.txt?True

刪除文件

file_path.unlink()

目錄創(chuàng)建和刪除:

創(chuàng)建多級(jí)目錄時(shí),要設(shè)置 mkdir 的 parent 參數(shù)為 True,比如 path.mkdir(parent=True)

test_folder?=?p?/?'Test?Folder'
test_folder.mkdir()

刪除,如果目錄不為空會(huì)報(bào)錯(cuò)

test_folder.rmdir()

如果要?jiǎng)h除多級(jí)目錄,可以用 os.removedirs(path),python 3.6之后支持傳入 Path對(duì)象


Python 目錄和文件操作 - pathlib的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
扎囊县| 镇康县| 广河县| 衡山县| 河间市| 肃北| 玛多县| 宽城| 花垣县| 额尔古纳市| 蓬溪县| 阜宁县| 博野县| 双柏县| 灵宝市| 怀集县| 保山市| 临朐县| 荃湾区| 丹寨县| 鄂托克前旗| 金塔县| 许昌市| 宿州市| 周至县| 灌南县| 竹溪县| 南平市| 易门县| 昂仁县| 交城县| 仙桃市| 慈溪市| 永州市| 象山县| 巴东县| 郑州市| 广宗县| 登封市| 郯城县| 行唐县|