将目录结构E:\采集图像\20200819\15\0000000013\ID_1_image_01_2020819152020888.png 文件按照类别进行分类存放
import os
from shutil import copyfile
import time
def getPartFile(pathstr, desPath):
# 创建目标文件夹
# os.path.exists()
subDirs = ["01","02","03","04"]
for item in subDirs:
subpath = desPath + "\\" +item;
if not os.path.exists(subpath):
os.makedirs(subpath)
dayDirsLst = os.listdir(pathstr)
# 日期文件夹处理
for dayDir in dayDirsLst:
print("\r\n[日期文件夹]" + dayDir)
# 日期路径
dayDirPath = pathstr + "\\" + dayDir
HourDirslst = os.listdir(dayDirPath)
# 小时文件夹处理
for hourdir in HourDirslst:
print("\t[小时文件夹]" + hourdir)
groupPath = dayDirPath + "\\" + hourdir
grouplst = os.listdir(groupPath)
for group in grouplst:
# 组
print("\t\t[组文件夹]" + group)
filePath = groupPath + "\\" + group
filelst = os.listdir(filePath)
# print("")
for file in filelst:
# print("处理文件:"+file)
fileDa = file.split("_")
if fileDa[3] == "01":
print(" \t\t\t处理第01部分"+file)
copyfile(filePath+"\\"+file, desPath +"\\01\\"+file)
elif fileDa[3] == "02":
print(" \t\t\t处理第02部分"+file)
copyfile(filePath+"\\"+file, desPath +"\\02\\"+file)
elif fileDa[3] == "03":
print(" \t\t\t处理第03部分"+file)
copyfile(filePath+"\\"+file, desPath +"\\03\\"+file)
elif fileDa[3] == "04":
print(" \t\t\t处理第04部分"+file)
copyfile(filePath+"\\"+file, desPath +"\\04\\"+file)
srcPath = "E:\\采集图像"
desPath = "E:\\odceshi\\new"
start = time.clock()
getPartFile(srcPath, desPath)
end = time.clock()
print( end -start)