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

 更新时间:2019年01月07日 08:56:43   作者:正经男大学生BRLF  
今天小编就为大家分享一篇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实现将多个文件分配到多个文件夹的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • django框架创建应用操作示例

    django框架创建应用操作示例

    这篇文章主要介绍了django框架创建应用操作,结合实例形式分析了Django框架的安装、创建项目、创建数据库及创建应用相关操作技巧,需要的朋友可以参考下
    2019-09-09
  • Python Bleach保障网络安全防止网站受到XSS(跨站脚本)攻击

    Python Bleach保障网络安全防止网站受到XSS(跨站脚本)攻击

    Bleach 不仅可以清理 HTML 文档,还能够对链接进行处理,检查是否是合法格式,并可以使用白名单来控制哪些 HTML 标签、属性是安全的,因此非常适合用于清洁用户输入的数据,确保网站安全
    2024-01-01
  • python性能检测工具函数运行内存及运行时间

    python性能检测工具函数运行内存及运行时间

    这篇文章主要介绍了python性能检测工具函数运行内存及运行时间,python虽然是一门慢语言,但是也有着比较多的性能检测工具来帮助我们优化程序的运行效率,下文小编给大家分享五个性能检测工具,需要的朋友可以参考一下
    2022-05-05
  • 使用Python分析数据并进行搜索引擎优化的操作步骤

    使用Python分析数据并进行搜索引擎优化的操作步骤

    在互联网时代,网站数据是一种宝贵的资源,可以用来分析用户行为、市场趋势、竞争对手策略等,本文将介绍如何使用Python爬取网站数据,并进行搜索引擎优化,,需要的朋友可以参考下
    2023-08-08
  • python判断字符串或者集合是否为空的实例

    python判断字符串或者集合是否为空的实例

    今天小编就为大家分享一篇python判断字符串或者集合是否为空的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • 基于Python制作一个文本翻译器

    基于Python制作一个文本翻译器

    translate非标准库是python中可以实现对多种语言进行互相翻译的库,本文就将利用这个库制作一个文本翻译器,实现中译英的功能,需要的可以参考一下
    2022-04-04
  • python 爬取免费简历模板网站的示例

    python 爬取免费简历模板网站的示例

    这篇文章主要介绍了python 爬取免费简历模板网站的示例,帮助大家更好的理解和使用python 爬虫,感兴趣的朋友可以了解下
    2020-09-09
  • Python使用plt.boxplot() 参数绘制箱线图

    Python使用plt.boxplot() 参数绘制箱线图

    这篇文章主要介绍了Python使用plt.boxplot() 参数绘制箱线图 ,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Python编程matplotlib绘图挑钻石seaborn小提琴和箱线图

    Python编程matplotlib绘图挑钻石seaborn小提琴和箱线图

    这篇文章主要为大家介绍了Python编程如何使用matplotlib绘图来挑出完美的钻石以及seaborn小提琴和箱线图,有需要的朋友可以借鉴参考下,希望能够优速帮助
    2021-10-10
  • 详解Django3中直接添加Websockets方式

    详解Django3中直接添加Websockets方式

    这篇文章主要介绍了Django3中直接添加Websockets方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02

最新评论