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)
输出结果:
原文地址:https://www.cnblogs.com/picassooo/p/12073781.html
时间: 2024-12-10 18:58:31