批量文件B中选出部分文件(与A文件夹数量相同),放到C中
import glob import os,sys import shutil fileDir = 'F:/project/Breast/InBreast/INBreast/outimgpatch/no/' #源图片文件夹路径 numDir = 'F:/project/Breast/InBreast/INBreast/outimgpatch/calcification/' tarDir = 'F:/project/Breast/InBreast/INBreast/outimgpatch/nocalcification/' #移动到新的文件夹路径 import os, random, shutil def moveFile(numd1,numd2,numd): #(fileDir,numDir) pathDir = os.listdir(numd1) #取图片的原始路径 filenumber=len(pathDir) pathDir1 = os.listdir(numd) # 取图片的原始路径 filenumber1 = len(pathDir1) print(filenumber,filenumber1) #rate=0.1 #自定义抽取图片的比例,比方说100张抽10张,那就是0.1 picknumber=filenumber1#按照rate比例从文件夹中取一定数量图片 sample = random.sample(pathDir, picknumber) #随机选取picknumber数量的样本图片 print (sample) i = 1 for name in sample: shutil.copy(numd1 + name, numd2+name[0:9]+str(i)+'.png') i = i+1 return if __name__ == '__main__': txtLists = os.listdir(numDir) # 列出文件夹下所有的目录与文件 for filename in txtLists: print(filename) numd = numDir + filename + '/' numd2 = tarDir + filename + '/' numd1 = fileDir + filename + '/' print(numd1) moveFile(numd1,numd2,numd)