python实现将多个文件分配到多个文件夹的方法

作者:正经男大学生BRLF 时间:2023-04-13 20:48:46 

如下所示:


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)

来源:https://blog.csdn.net/u011554976/article/details/80615638

标签:python,文件,文件夹
0
投稿

猜你喜欢

  • layui radio单选限制下一个radio单选的实例

    2024-06-09 15:52:07
  • Python pyinotify日志监控系统处理日志的方法

    2023-12-11 08:57:37
  • Python filter()及reduce()函数使用方法解析

    2022-09-05 07:01:59
  • 通过javascript进行UTF-8编码的实现方法

    2024-04-16 10:39:49
  • pandas函数isnull的具体使用

    2022-08-04 18:43:02
  • 解决pytorch中的kl divergence计算问题

    2023-11-12 11:02:00
  • Python采集代理ip并判断是否可用和定时更新的方法

    2022-12-01 15:19:05
  • 对Keras自带Loss Function的深入研究

    2021-08-27 03:18:24
  • 使用Python实现企业微信的自动打卡功能

    2021-02-27 02:41:34
  • Python中pandas dataframe删除一行或一列:drop函数详解

    2021-07-09 16:46:47
  • 如何在Python中妥善使用进度条详解

    2022-05-30 12:10:05
  • Python Decorator装饰器的创建方法及常用场景分析

    2022-05-05 03:34:11
  • linux安装Python3.4.2的操作方法

    2022-06-17 19:19:15
  • JavaScript原生对象常用方法总结(推荐)

    2024-05-05 09:15:20
  • vue如何通过params和query传值(刷新不丢失)

    2024-05-09 15:17:23
  • MybatisPlus二级缓存体系探究分析

    2022-12-24 00:42:26
  • 浅析vue-router原理

    2023-07-02 17:09:19
  • 2008年情人节各网站节日Logo欣赏

    2008-02-17 14:12:00
  • Linux环境下安装mysql5.7.36数据库教程

    2024-01-19 15:42:02
  • rs.open sql,conn,1,1与rs.open sql,conn,1.3还有rs.open sql,conn,3,2区别

    2011-02-24 10:49:00
  • asp之家 网络编程 m.aspxhome.com