python中可以用os.walk来遍历某个文件夹中所有文件夹和文件。
例1:
import os
filePath = 'C:/Users/admin/Desktop/img'
for dirpath, dirnames, filenames in os.walk(filePath):
print(dirpath, dirnames, filenames)
输出结果:
例2:
import os
filePath = 'C:\\Users\\admin\\Desktop\\img'
for dirpath, dirnames, filenames in os.walk(filePath):
path = [os.path.join(dirpath, names) for names in filenames]
print(path)
输出结果: