def random_copyfile(srcPath,dstPath):
for dir_info in os.walk(srcPath):
dir_name, _, file_names = dir_info
temp = []
for file_name in file_names:
temp.append(file_name)
if len(temp) == 0:
continue
randomfile = random.choice(temp)
I = Image.open(os.path.join(dir_name,randomfile))
I.save(os.path.join(dstPath,temp[0]))
if __main__ == '__main__':
srcPath='.//img'
dstPath = './/all_img'
random_copyfile(srcPath,dstPath)
你代码貌似已经是这种逻辑啦。
大概逻辑
1、写个过程 选择目录下的随机文件,复制到指定目录
2、遍历工作目录下的所有子目录到列表
遍历在这个列表, 调用1代码
建议用插入代码功能贴下代码