本段python``代码可以遍历某个文件夹及其下所有子目录文件夹。
例如对所有文件重命名等。
import os data_dir = "target_dir" path = "/workspace/.../Data/" def getFileList(dir, Filelist, ext=None): newDir = dir if os.path.isfile(dir): if ext is None: Filelist.append(dir) #or you can quit the program else: if ext in dir[-3:]: try: #你想对目标文件执行的操作 os.rename(path + dir, path + dir.replace('.png', '.jpg')) except Exception as e: print(e) else: #操作成功print log print("rename" + dir + "success") Filelist.append(dir) elif os.path.isdir(dir): for s in os.listdir(dir): newDir = os.path.join(dir,s) getFileList(newDir, Filelist, ext) return Filelist imglist = getFileList(data_dir, [], 'png')