# author by LYS 2017/5/24
# for Deep Learning course
'''
1. read the whole files under a certain folder
2. chose 10000 files randomly
3. copy them to another folder and save
'''
import os, random, shutil
def copyFile(fileDir,tarDir):
# 1
pathDir = os.listdir(fileDir)
# 2
sample = random.sample(pathDir, 10000)
print sample
# 3
for name in sample:
shutil.copyfile(fileDir+name, tarDir+name)
if __name__ == '__main__':
fileDir = "/home/lys/img_align_celeba_png/"
tarDir = '/home/lys/celebA/'
copyFile(fileDir,tarDir)
python随机选取10000张图片并复制到另一个文件夹中
遇到的问题:
shell命令行下for循环内部要有四个空格;回车两次for循环执行;copy文件时,目录最后要有/,否则直接把文件名字添加在了目录后面,更改路径
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.