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


Posted in Python onJanuary 07, 2019

如下所示:

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实现将多个文件分配到多个文件夹的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python模块之StringIO使用示例
Apr 08 Python
Python实现将罗马数字转换成普通阿拉伯数字的方法
Apr 19 Python
Python动态导入模块的方法实例分析
Jun 28 Python
Python 利用内置set函数对字符串和列表进行去重的方法
Jun 29 Python
python之文件读取一行一行的方法
Jul 12 Python
介绍一款python类型检查工具pyright(推荐)
Jul 03 Python
将Pytorch模型从CPU转换成GPU的实现方法
Aug 19 Python
Python对接支付宝支付自实现功能
Oct 10 Python
python中有关时间日期格式转换问题
Dec 25 Python
Python+unittest+requests 接口自动化测试框架搭建教程
Oct 09 Python
用pip给python安装matplotlib库的详细教程
Feb 24 Python
如何正确理解python装饰器
Jun 15 Python
在python中使用with打开多个文件的方法
Jan 07 #Python
python读取文件名并改名字的实例
Jan 07 #Python
Python 调用 zabbix api的方法示例
Jan 06 #Python
使用Django2快速开发Web项目的详细步骤
Jan 06 #Python
利用Django提供的ModelForm增删改数据的方法
Jan 06 #Python
Python面向对象类编写细节分析【类,方法,继承,超类,接口等】
Jan 05 #Python
Python面向对象程序设计OOP深入分析【构造函数,组合类,工具类等】
Jan 05 #Python
You might like
微博短链接算法php版本实现代码
2012/09/15 PHP
php生成缩略图示例代码分享(使用gd库实现)
2014/01/20 PHP
动态加载iframe
2006/06/16 Javascript
javascript 清除输入框中的数据
2009/04/13 Javascript
javascript下string.format函数补充
2010/08/24 Javascript
jQuery插件bgStretcher.js实现全屏背景特效
2015/06/05 Javascript
js随机生成字母数字组合的字符串 随机动画数字
2015/09/02 Javascript
JS实现常见的TAB、弹出层效果(TAB标签,斑马线,遮罩层等)
2015/10/08 Javascript
超简单的Vue.js环境搭建教程
2017/03/17 Javascript
jQuery实现导航栏头部菜单项点击后变换颜色的方法
2017/07/19 jQuery
深入掌握 react的 setState的工作机制
2017/09/27 Javascript
vue scroller返回页面记住滚动位置的实例代码
2018/01/29 Javascript
python 数据加密代码
2008/12/24 Python
python使用7z解压软件备份文件脚本分享
2014/02/21 Python
ptyhon实现sitemap生成示例
2014/03/30 Python
Python的装饰器模式与面向切面编程详解
2015/06/21 Python
两个使用Python脚本操作文件的小示例分享
2015/08/27 Python
Python数据类型详解(一)字符串
2016/05/08 Python
django model去掉unique_together报错的解决方案
2016/10/18 Python
python+django快速实现文件上传
2016/10/24 Python
详解python的ORM中Pony用法
2018/02/09 Python
Python 创建空的list,以及append用法讲解
2018/05/04 Python
python 遍历列表提取下标和值的实例
2018/12/25 Python
使用Python将Mysql的查询数据导出到文件的方法
2019/02/25 Python
Python计算IV值的示例讲解
2020/02/28 Python
你需要学会的8个Python列表技巧
2020/06/24 Python
HTML5 绘制图像(上)之:关于canvas元素引领下一代web页面的问题
2013/04/24 HTML / CSS
英国家用电器购物网站:Hughes
2018/02/23 全球购物
Wedgwood英国官方网站:英式精致骨瓷餐具、礼品与生活精品,源于1759年
2019/09/02 全球购物
Farfetch澳大利亚官网:Farfetch Australia
2020/04/26 全球购物
关于Java String的一道面试题
2013/09/29 面试题
MIS软件工程师的面试题
2016/04/22 面试题
安全检查验收制度
2014/01/12 职场文书
公司接待方案
2014/03/08 职场文书
公务员个人考察材料
2014/12/23 职场文书
使用CSS定位HTML元素的实现方法
2022/07/07 HTML / CSS