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

所属分类: 脚本专栏 / python 阅读数: 1881
收藏 0 赞 0 分享

如图所示,有一个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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

python2.7无法使用pip的解决方法(安装easy_install)

下面小编就为大家分享一篇python2.7无法使用pip的解决方法(安装easy_install),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python实现的计算马氏距离算法示例

这篇文章主要介绍了Python实现的计算马氏距离算法,简单说明了马氏距离算法原理,并结合实例形式分析了Python实现与使用马氏距离算法的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python逐行读写txt文件的实例讲解

下面小编就为大家分享一篇python逐行读写txt文件的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python批量读取txt文件为DataFrame的方法

下面小编就为大家分享一篇python批量读取txt文件为DataFrame的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python通过调用mysql存储过程实现更新数据功能示例

这篇文章主要介绍了Python通过调用mysql存储过程实现更新数据功能,结合实例形式分析了Python调用mysql存储过程实现更新数据的具体步骤与相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现的HMacMD5加密算法示例

这篇文章主要介绍了Python实现的HMacMD5加密算法,简单说明了HMAC-MD5加密算法的概念、原理并结合实例形式分析了Python实现HMAC-MD5加密算法的相关操作技巧,,末尾还附带了Java实现HMAC-MD5加密算法的示例,需要的朋友可以参考下
收藏 0 赞 0 分享

图解Python变量与赋值

Python是一门独特的语言,与C语言有很大区别,初学Python很多萌新表示对变量与赋值不理解,这里就大家介绍一下,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的并发处理之asyncio包使用的详解

本篇文章主要介绍了Python中的并发处理之asyncio包使用的详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python获取二维矩阵每列最大值的方法

下面小编就为大家分享一篇Python获取二维矩阵每列最大值的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

numpy找出array中的最大值,最小值实例

下面小编就为大家分享一篇numpy找出array中的最大值,最小值实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多