当前位置:
首页 > Python基础教程 >
-
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(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实现将多个文件分配到多个文件夹的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/u011554976/article/details/80615638
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
开启SQLSERVER数据库缓存依赖优化网站性能
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比