import os
import shutil
import math
defdatabag(path,folderPath, number):
path = path.strip("\"")
file_list = os.listdir(path)#源文件名称列表
Number = math.ceil(len(file_list)/ number)#目标文件夹数量
folderNumber =-1#起始文件夹id ,-1是因为0 % 任意数 = 0
sort_folder_number =[x for x inrange(0,Number)]# 创建文件夹for foldernumber in sort_folder_number:
new_folder_path = os.path.join(folderPath,'%s'%foldernumber)#new_folder_path is ‘folderPath\number'ifnot os.path.exists(new_folder_path):
os.makedirs(new_folder_path)print("new a floder named "+str(foldernumber)+'at the path of '+ new_folder_path)#分包for i inrange(0,len(file_list)):
old_file = os.path.join(path, file_list[i])if os.path.isdir(old_file):'''if the path is a folder,program will pass it'''print('img does not exist ,path='+ old_file+' it is a dir')passelifnot os.path.exists(old_file):'''if the path does not exist,program will pass it'''print('img does not exist ,path='+old_file)passelse:'''define the number,it decides how many imgs each people process'''if(0==(i % number)):#导致folderNumber = -1 : 0 % 任意数 = 0
folderNumber +=1
new_file_path = os.path.join(folderPath,'%s'%(folderNumber))ifnot os.path.exists(new_file_path):break
shutil.move(old_file,new_file_path)print(old_file+'is successfully moved to '+new_file_path)
class_names = os.listdir('D:\PycharmProjects\Classification1\datasets/train3')for i inrange(20):
flodername = os.listdir('D:\PycharmProjects\Classification1\datasets/train3/'+class_names[i])
path='D:\PycharmProjects\Classification1\datasets/train3/'+class_names[i]
databag(path, path,100)print(path)'''file_list = os.listdir(path)
print(file_list)''''''for p in range(100):
des='D:\PycharmProjects\Classification1\datasets/train3/'+class_names[i]+'/0/'+file_list[p]
print(des)'''