Python脚本:将一个大文件夹均分为小文件夹
程序员文章站
2022-05-18 20:29:39
...
'''
demo功能说明:
1.在folderPath处新建10个文件夹,分别为0-9,每个文件夹里有一对Annotations和JEPGImages文件夹。
2.源标注和源图片分别存储在path和JPEGImages_path处
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
'''
import os
import shutil
#path of imgr
path = '/home/xl/workstation/Datasets/annores/XML_Annotations/'
JPEGImages_path = '/home/xl/workstation/Datasets/annores/JPEGImages/'
#path of folder
folderPath = '/home/xl/workstation/Datasets/annores/folderSort2/'
# number of folder
peopleNumber = 10
#name of folder:0,1,2
sort_folder_number = [x for x in range(0,peopleNumber)]
#give the img list
file_list = os.listdir(path)
#list of
f_list=list()
#the number of one folder
part_number = int(len(file_list)/peopleNumber)
#length of file_list
len=len(file_list)
# 初始化
folderNumber=0
i=0
#所有图片的前缀
for f in file_list:
portion=os.path.splitext(f)
if portion[1] == '.xml':
f2=portion[0]
f_list.append(f2)
# makedir 10 folders
for number in sort_folder_number:
new_folder_path_xml = os.path.join(folderPath,'%s'%number+"/Annotations/")#new_folder_path is ‘folderPath\number'
new_folder_path_jpg = os.path.join(folderPath,'%s'%number+"/JPEGImages/")
if not os.path.exists(new_folder_path_xml) :
os.makedirs(new_folder_path_xml)
if not os.path.exists(new_folder_path_jpg):
os.makedirs(new_folder_path_jpg)
print("new a floder named "+str(number)+'at the path of '+ new_folder_path_xml)
print("new a floder named " + str(number) + 'at the path of ' + new_folder_path_jpg)
'''define the number,it decides how many imgs each people process'''
for f in f_list:
if(i%part_number ==0) and i:
i = i + 1
break
else:
p_ano= os.path.join(path,f+".xml")
p_pic= os.path.join(JPEGImages_path, f + ".jpg")
shutil.copy(p_ano,new_folder_path_xml)
shutil.copy(p_pic,new_folder_path_jpg)
i =i+1
真心觉得知识才是第一生产力????
上一篇: VUE 中的搜索关键字
下一篇: Java中关系操作符的实例详解