比如,某一个文件夹中一共有一万张图片,你想取其中的五千张,复制到另一个文件夹之下,该如何操作呢?用Python实现起来很简单。
import shutil
import os
#目录自己改一下即可
path = "/media/xxx/KINGIDISK/kaggle_driving_imgs/train/c9/" #there are many directories
new_path = "/home/xxx/Documents/tem/smallDataset/train/trc9/"
count = 0
for file in os.listdir(path):
full_file = os.path.join(path, file)
new_full_file = os.path.join(new_path, file)
shutil.copy(full_file, new_full_file)
print(count)
if count == 499:
print("done!")
break
count += 1
print("complete!")