1. walk() 爬树式遍历文件
文件存储位置图:
举例,每个文件下面都有:
# coding:utf-8
import os
#walk 爬树式遍历文件
def walkToGrabDocuments():
for dir_path, dir_names, file_names in os.walk('D:/PythonFiles/2018second/speechSplit/recordings/'):
# walk() 函数内存放的是数据的绝对路径,同时注意斜杠的方向。
print("地址: " + dir_path) # 所指的是当前正在遍历的这个文件夹的本身的地址
print("目录名: ", end=' ') # 目录名
print(dir_names)
print("文件名: ", end=' ') # 文件名
print(file_names)
print('====================')
for fn in file_names:
print(fn)
if __name__ == '__main__':
walkToGrabDocuments()
输出:
D:\Python35\python3.exe
地址: D:/PythonFiles/2018second/speechSplit/recordings/
目录名: ['2018-09-25', '2018-09-26']
文件名: []
====================
地址: D:/PythonFiles/2018second/speechSplit/recordings/2018-09-25
目录名: []
文件名: ['00a9f3f2-2b9b-4dc0-a55f-d5d472701e4e.wav', '0bb2cc0b-d921-4ad6-a579-87b72385dff9.wav', '0c092302-7e35-4508-9429-32f4888cd27b.wav', '1ae0898d-93a3-4ef4-a582-e460c37ad612.wav', '1cd8f4a7-8c5e-4746-b61a-f368376a2283.wav', '1d4e0c47-98c2-4650-b59c-2d9ea4198cce.wav', '1d7f0a1e-91b1-4681-80c1-eabc494017d6.wav', '1e0f90a1-f6d9-4551-9469-c87b564f142f.wav', '1e9e458c-b3b0-46ba-8a67-151465453655.wav']
====================
00a9f3f2-2b9b-4dc0-a55f-d5d472701e4e.wav
0bb2cc0b-d921-4ad6-a579-87b72385dff9.wav
0c092302-7e35-4508-9429-32f4888cd27b.wav
1ae0898d-93a3-4ef4-a582-e460c37ad612.wav
1cd8f4a7-8c5e-4746-b61a-f368376a2283.wav
1d4e0c47-98c2-4650-b59c-2d9ea4198cce.wav
1d7f0a1e-91b1-4681-80c1-eabc494017d6.wav
1e0f90a1-f6d9-4551-9469-c87b564f142f.wav
1e9e458c-b3b0-46ba-8a67-151465453655.wav
地址: D:/PythonFiles/2018second/speechSplit/recordings/2018-09-26
目录名: []
文件名: ['0bde823a-acd7-4ab9-b4a6-0c76c9b4d1d0.wav', '0d73c422-a55f-40bf-b0d7-69a099b8079c.wav', '0dc1ca70-1164-4d9c-ac24-b54c0cf48e30.wav', '0e849ee7-ea9a-4e70-9a76-6a18333d578f.wav', '0f177a50-a6cf-4a6c-be2f-937eafb18da6.wav', '0f1c6267-6323-4f54-8572-77c5416ba231.wav', '1a1f6451-74ff-4032-94c5-30f5f4fc83db.wav', '1a466afc-8ac5-4c9d-9d72-83a6d7998a57.wav', '1b541517-94d1-4fda-aaa5-8be72a93273f.wav', '1beebb59-8bfa-4635-9231-81a733cd5c03.wav', '1c4368ba-d383-40b1-b258-0d6e1c9c73de.wav', '1c71ad4c-b6a0-4823-a26b-86b4c1f42311.wav', '1c976de3-d5c9-4c82-a879-decb35ad0888.wav', '1cb0a554-cd65-4fcb-bab3-0288f7b9eff6.wav', '1d807649-b389-4a8c-b9cc-37aa74b8de93.wav']
====================
0bde823a-acd7-4ab9-b4a6-0c76c9b4d1d0.wav
0d73c422-a55f-40bf-b0d7-69a099b8079c.wav
0dc1ca70-1164-4d9c-ac24-b54c0cf48e30.wav
0e849ee7-ea9a-4e70-9a76-6a18333d578f.wav
0f177a50-a6cf-4a6c-be2f-937eafb18da6.wav
0f1c6267-6323-4f54-8572-77c5416ba231.wav
1a1f6451-74ff-4032-94c5-30f5f4fc83db.wav
1a466afc-8ac5-4c9d-9d72-83a6d7998a57.wav
1b541517-94d1-4fda-aaa5-8be72a93273f.wav
1beebb59-8bfa-4635-9231-81a733cd5c03.wav
1c4368ba-d383-40b1-b258-0d6e1c9c73de.wav
1c71ad4c-b6a0-4823-a26b-86b4c1f42311.wav
1c976de3-d5c9-4c82-a879-decb35ad0888.wav
1cb0a554-cd65-4fcb-bab3-0288f7b9eff6.wav
1d807649-b389-4a8c-b9cc-37aa74b8de93.wav
2.python os.listdir()
python os.path模块(os.listdir和os._exit())
参考资料:
python的walk及图片转换格式
网友评论