python实现将多个文件分配到多个文件夹的方法
作者:正经男大学生BRLF 发布时间:2023-04-13 20:48:46
标签: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)
来源:https://blog.csdn.net/u011554976/article/details/80615638


猜你喜欢
- 本文实例讲述了Python聚类算法之DBSACN。分享给大家供大家参考,具体如下:DBSCAN:是一种简单的,基于密度的聚类算法。本次实现中
- Python 3.10.0a2 版本已经于 2020-11-04 发布,因此我们可以窥见 Python 3.10 的一些新特性。这些新特性很
- 本文实例讲述了python中MySQLdb模块用法。分享给大家供大家参考。具体用法分析如下:MySQLdb其实有点像php或asp中连接数据
- 环境:python3.6 pyqt5只是简单的一个思路,请忽略脆弱的异常防护:# -*- coding: utf-8 -*-import s
- 15分钟学会vue项目改造成SSRPs:网上看了好多服务器渲染的例子,基本都是从0开始的,用Nuxt或者vue官网推荐的ssr方案(vue-
- 关于主机名转IP地址只记住两点即可:1、英特网协议一直都是用4字节的IP将包转发给目的地;2、至于主机名是如何转换成IP地址的,这是操作系统
- 如下所示:#在/etc/my.cnf中的[mysqld]中加入如下代码:slow-query-log=Onslow_query_log_fi
- 1.生成器# 一边循环一边计算的机制,称为生成器:generator;# 创建generator方法:# 1.把一个列表生成式的[]改成()
- 对我当前工程进行全部测试需要花费不少时间。既然有 26 GB 空闲内存,为何不让其发挥余热呢? tmpfs 可以通过把文件系统保
- 将一个 awk 脚本移植到 Python 主要在于代码风格而不是转译。脚本是解决问题的有效方法,而 awk 是编写脚本的出色语言。它特别擅长
- 有这样一个文本文件,内容有多行如下,数量不定。Lif(__amscript_cd("www.jb51.net")){__
- 张量是一种特殊的数据结构,与数组和矩阵非常相似。在 PyTorch 中,我们使用张量对模型的输入和输出以及模型的参数进行编码。张量类似于Nu
- 图像标注在计算机视觉中很重要,计算机视觉是一种技术,它允许计算机从数字图像或视频中获得高水平的理解力,并以人类的方式观察和解释视觉信息。注释
- 傅里叶变换图像处理一般分为空间域处理和频率域处理。空间域处理是直接对图像内的像素进行处理。空间域处理主要划分为灰度变换和空间滤波两种形式。灰
- bookheader.asp Recommended Books for <%=session(&quo
- asp代码 如下:读取注册表信息使用了对象WScript.Shell<%Dim strPath strP
- 后台数据库用是Access,客户用了一年后说打开界面非常慢,查看了数据库后发现数据表中的记录已有五万多条,自己试过将记录复制到10 万条,打
- 制作自己的训练集下图是我们数据的存放格式,在data目录下有验证集与测试集分别对应iris_test, iris_train 为了向伟大的M
- model.pyimport datetimefrom django.contrib.auth.models import Userfrom
- 在来回切换中英文输入法的时候连按两下shift总是会蹦出来全局搜索框真的很是麻烦,现在是把这个框给禁用掉1.按ctrl+shift+a,弹出