网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python实现将多个文件分配到多个文件夹的方法-创新互联

如下所示:

在云溪等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、成都网站制作 网站设计制作按需网站设计,公司网站建设,企业网站建设,成都品牌网站建设,网络营销推广,外贸营销网站建设,云溪网站建设费用合理。
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(path2,path3...)
this function is used to combine the path,it returns a path which is 'path2/path3...'

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实现将多个文件分配到多个文件夹的方法-创新互联
本文路径:http://bjjierui.cn/article/jopsd.html

其他资讯