Python 目录和文件操作 - pathlib
面向对象的目录、文件系统模块,可以取代os.path
导入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 # 控制展示结果数量
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 # 控制展示结果数量
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
创建文件:
file_path.touch() # 只要不报错,就是创建成功
file_path.exists() and file_path.is_file()
True
文件读写:
# 和python 内置函数 open()类似
with file_path.open('w',encoding='utf-8') as f:
f.write('Hello!')
# 写入字符串更简单的用法,文件会自动关闭
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')
# 二进制读写可用 write_bytes(data), read_bytes()
'Hello'
文件重命名和删除:
用with_stem返回一个新路径,作为重命名的目标参数,也可以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,并把返回的路径对象赋值给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()
目录创建和删除:
创建多级目录时,要设置 mkdir 的 parents 参数为 True,比如 path.mkdir(parents=True)
test_folder = p / 'Test Folder'
test_folder.mkdir()
删除,如果目录不为空会报错
test_folder.rmdir()