pythonは10000枚の画像をランダムに選択して別のフォルダにコピーします

733 ワード

# 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):
    # 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)

問題:
shellコマンドラインの下のforループの内部には4つのスペースが必要です.リターン2回forサイクル実行;copyファイルの場合、ディレクトリは最後に/が必要です.そうしないと、直接ファイル名をディレクトリの後ろに追加し、パスを変更します.