DDR爱好者之家 Design By 杰米
如下所示:
import os import shutil #path of imgr path = 'D:\\BaiduNetdiskDownload\\newim\\' #path of folder folderPath = 'D:\\BaiduNetdiskDownload\\folderSort\\' peopleNumber = 61 #new 61 folder numbers as sort_folder_number[61] sort_folder_number = [x for x in range(0,peopleNumber)] # makedir 61 folders ''' demo功能说明: 在folderPath处新建60个文件夹, 图片存储在path处 给每个文件夹分配150张图片(将9000张图片平均分配到60个文件夹) Tips: 1: os.path.join(path1,path2...) this function is used to combine the path,it returns a path which is 'path1/path2...' 2: os.makedirs(path) this function is used to make a directory(new folder) in the path param 3: shutil.move(oldPath,newPath) this function is used to move file from param1 to param 2 4: os.path.exists(path) this function is used to check the filePath(param1) whether exists ''' for number in sort_folder_number: new_folder_path = os.path.join(folderPath,'%s'%number)#new_folder_path is ‘folderPath\number' if not os.path.exists(new_folder_path): os.makedirs(new_folder_path) print("new a floder named "+str(number)+'at the path of '+ new_folder_path) #give the img list file_list = os.listdir(path) '''define the first foloderNumber''' folderNumber = 1 print('there are '+str(len(file_list))+' files at the path of '+path) for i in range(0,len(file_list)): old_file_path = os.path.join(path,str(i)+'.jpg') if os.path.isdir(old_file_path): '''if the path is a folder,program will pass it''' print('img does not exist ,path=' + old_file_path+' it is a dir' ) pass elif not os.path.exists(old_file_path): '''if the path does not exist,program will pass it''' print('img does not exist ,path='+old_file_path) pass else: '''define the number,it decides how many imgs each people process''' number = 150 #int(len(file_list)/peopleNumber) if(i%number ==0): folderNumber +=1 new_file_path = os.path.join(folderPath,'%s'%(folderNumber)) if not os.path.exists(new_file_path): print('not exist path:'+new_file_path) break shutil.move(old_file_path,new_file_path) print('success move file from '+ old_file_path +' to '+new_file_path)
以上这篇python实现将多个文件分配到多个文件夹的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月27日
2024年11月27日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]