python 文件分类

将目录结构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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值