如图所示,有一个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文件夹批处理操作的更多相关文章

  1. python之文件的读写和文件目录以及文件夹的操作实现代码

    这篇文章主要介绍了python之文件的读写和文件目录以及文件夹的操作实现代码,需要的朋友可以参考下 为了安全起见,最好还是给打开的文件对象指定一个名字,这样在完成操作之后可以迅速关闭文件,防止一些无用 ...

  2. python 关于文件夹的操作

    在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() #  path 表示文 ...

  3. Python 文件夹及文件操作

    import os import os.path from shutil import copy def copyfile(src, dst): count = 1 for filename in o ...

  4. [转]python中对文件、文件夹的操作——os模块和shutil模块常用说明

    转至:http://l90z11.blog.163.com/blog/static/187389042201312153318389/ python中对文件.文件夹的操作需要涉及到os模块和shuti ...

  5. python中对文件、文件夹的操作

    python中对文件.文件夹的操作需要涉及到os模块和shutil模块. 创建文件: 1) os.mknod("test.txt")       创建空文件 2) open(&qu ...

  6. java io流 对文件夹的操作

    java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...

  7. Python文件夹备份

    Python文件夹备份 import os,shutil def file_copy(path1,path2): f2 = [filename1 for filename1 in os.listdir ...

  8. Qt: 文件、文件夹的操作;

    Qt没有提供单独的函数来对文件.文件夹进行操作, 但是提供了两个类: QFile, QDir; 1.文件操作 ) 文件是否存在: QFile file("D:/test.jpg") ...

  9. JAVA文件操作类和文件夹的操作代码示例

    JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...

随机推荐

  1. 以 DirectUI 方式实现的ImageButton

    原文链接: http://www.cnblogs.com/hoodlum1980/archive/2011/02/15/1954779.html 这是一篇比较简单的文章,主要讲解的是用 DirectU ...

  2. 记录一次条件比较多的SQL查询语句

    本人目前遇到一个比较长的查询语句: (个人觉得越是复杂的查询越有可能意味着数据库设计的不太合理,非数据领域专业人士,仅个人感觉)

  3. django中models阅读笔记

    一.使用数据库需要设置settings.py文件. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.', # Add 'postgre ...

  4. JavaScript:表单常用验证脚本(整理)

    以下内容根据网上资源整理而来,主要来源是CSDN一个供下载的check.js,源码地址找不到了. 1. 检查输入字符串是否为空或者全部都是空格 /* 检查输入字符串是否为空或者全部都是空格 输入:st ...

  5. window 2008 定时任务调用bat不成功的解决方法

    之前一直有在一台XP的机器上调用定时任务.如今这台机器换成了window 2008的操作系统,调用一直不成功.只是在偶然之间攻克了. 选择"任务计划程序"     任务计划程序库 ...

  6. 大数问题:求n的阶乘

    题目:求100! 这看起来是一个非常简答的问题,递归解之毫无压力 int func(int n){ if(n <= 1) return 1; else return n*func(n-1); } ...

  7. U3D正播反播动画剪辑

    正播就是直接play item.animation.Play("a"); 反播: item.animation.Play("a"); item.animatio ...

  8. Vector3.Set的正确使用

    直接调用position.Set会发现没有用.其实和其本身是结构体有关,要当成一个值类型来看待,因为他全部是在栈中. transform.position.Set(,,); 改成这样即可: var t ...

  9. Java NIO- Selector 使用示例

    Selector selector = Selector.open(); channel.configureBlocking(false); SelectionKey key = channel.re ...

  10. QT中布局器的addStretch函数使用效果

    QBoxLayout中addStretch 函数说明: void QBoxLayout::addStretch(int stretch = 0) Adds a stretchable space (a ...