Python 目錄和文件操作 - pathlib
目錄和文件操作 - 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?+=1E:\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?+=1E:\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()