欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python文件夹批处理操作代码实例

时间:2021-11-01 10:11:45|栏目:Python代码|点击:

如图所示,有一个test文件夹,里面有3个子文件夹,每个子文件夹中有若干图片文件

#场景1 读取一个文件夹中所有文件,存入到一个list表中

#coding:utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import os
import sys
import math
import numpy
import time
import argparse
import random
import cv2


def findAllfile(path, allfile):
  filelist = os.listdir(path) 
  for filename in filelist: 
    filepath = os.path.join(path, filename) 
    if os.path.isdir(filepath):
      #print(filepath) 
      findAllfile(filepath, allfile) 
    else: 
      allfile.append(filepath) 
  return allfile 

#场景1 读取一个文件夹中所有文件,存入到一个list表中
def process1(srcpath, imgprocess_result):

  #遍历图像文件夹
  image_files = findAllfile(srcpath,[])
  #image_files为所有文件的list

  #判断 目录是否存在,存在就删除,并且重建
  if os.path.exists(imgprocess_result):
    os.system("rm -rf " + imgprocess_result)
  if not os.path.isdir(imgprocess_result): # Create the log directory if it doesn't exist
    os.makedirs(imgprocess_result)

  #是否随机打乱文件顺序
  #random.shuffle(image_files)

  #遍历所有
  for facepath in image_files:
    print("原始文件路径:", facepath)
    #获取文件名称
    data_split = facepath.strip().split("/")
    image_floder = data_split[-2]
    print("文件所在目录:", image_floder)
    image_name = data_split[-1]
    print("文件名称:", image_name)

    image_newfloder = imgprocess_result + "/" + image_floder
    #判断 目录是否存在,不存在就重建
    if not os.path.isdir(image_newfloder): # Create the log directory if it doesn't exist
      os.makedirs(image_newfloder)

    image_newpath = image_newfloder + "/" + image_name
    print("处理后的文件路径:", image_newpath)

    #开始处理文件
    #..............
    #
    #


#场景2 首先读取一个文件夹中的所有子目录,然后依次遍历各个子目录的所有文件
def process2(srcpath, imgprocess_result):
  #找出所有的子目录
  filelist = os.listdir(srcpath) 
  for filename in filelist: 
    filepath = os.path.join(srcpath, filename) 
    if os.path.isdir(filepath):
      print("原始子目录路径:", filepath)
      image_files = findAllfile(filepath,[])
      for facepath in image_files:
        print("原始文件路径:", facepath)
        #获取文件名称
        data_split = facepath.strip().split("/")
        image_floder = data_split[-2]
        print("文件所在目录:", image_floder)
        image_name = data_split[-1]
        print("文件名称:", image_name)

        image_newfloder = imgprocess_result + "/" + image_floder
        #判断 目录是否存在,不存在就重建
        if not os.path.isdir(image_newfloder): # Create the log directory if it doesn't exist
          os.makedirs(image_newfloder)

        image_newpath = image_newfloder + "/" + image_name
        print("处理后的文件路径:", image_newpath)

        #开始处理文件
        #..............
        #
        #

if __name__ == '__main__':
  #原始文件夹
  srcpath = "/DATA/share/publicdata/face/dmt_face/test"
  #处理完毕后存放文件
  imgprocess_result = "/DATA/share/publicdata/face/dmt_face/imgprocess_result"

  print("方法1\n\n\n\n")
  process1(srcpath, imgprocess_result)
  print("\n\n\n方法2")
  process2(srcpath, imgprocess_result)

上一篇:Python中asyncio模块的深入讲解

栏    目:Python代码

下一篇:基于python实现学生信息管理系统

本文标题:Python文件夹批处理操作代码实例

本文地址:http://www.codeinn.net/misctech/182017.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有