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文件夹批处理操作的更多相关文章
- python之文件的读写和文件目录以及文件夹的操作实现代码
这篇文章主要介绍了python之文件的读写和文件目录以及文件夹的操作实现代码,需要的朋友可以参考下 为了安全起见,最好还是给打开的文件对象指定一个名字,这样在完成操作之后可以迅速关闭文件,防止一些无用 ...
- python 关于文件夹的操作
在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() # path 表示文 ...
- Python 文件夹及文件操作
import os import os.path from shutil import copy def copyfile(src, dst): count = 1 for filename in o ...
- [转]python中对文件、文件夹的操作——os模块和shutil模块常用说明
转至:http://l90z11.blog.163.com/blog/static/187389042201312153318389/ python中对文件.文件夹的操作需要涉及到os模块和shuti ...
- python中对文件、文件夹的操作
python中对文件.文件夹的操作需要涉及到os模块和shutil模块. 创建文件: 1) os.mknod("test.txt") 创建空文件 2) open(&qu ...
- java io流 对文件夹的操作
java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...
- Python文件夹备份
Python文件夹备份 import os,shutil def file_copy(path1,path2): f2 = [filename1 for filename1 in os.listdir ...
- Qt: 文件、文件夹的操作;
Qt没有提供单独的函数来对文件.文件夹进行操作, 但是提供了两个类: QFile, QDir; 1.文件操作 ) 文件是否存在: QFile file("D:/test.jpg") ...
- JAVA文件操作类和文件夹的操作代码示例
JAVA文件操作类和文件夹的操作代码实例,包括读取文本文件内容, 新建目录,多级目录创建,新建文件,有编码方式的文件创建, 删除文件,删除文件夹,删除指定文件夹下所有文件, 复制单个文件,复制整个文件 ...
随机推荐
- Python2 字典 has_key() 方法
描述 Python2 字典 has_key() 方法用于判断键(key)是否存在于字典(D)中,如果键在字典中返回True,否则返回False. 官方文档推荐用 in 操作符,因为它更短更通俗易懂.h ...
- zabbix应用之Low-level discovery监控磁盘IO
参考文章: http://qicheng0211.blog.51cto.com/3958621/1599776/ zabbix自带的"Template OS Linux"模板支持监 ...
- removeChildByTag、schedule、schedule_selector
Test4::Test4() { CCSprite *sp1 = CCSprite::create(s_pPathSister1); CCSprite *sp2 = CCSprite::create( ...
- Flink articles
http://ictlabs-summer-school.sics.se/2015/slides/flink-advanced.pdf http://henning.kropponline.de/20 ...
- javascript 图片上传缩略图预览
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- 在windows环境下编译hadoop
1.环境准备 1.1 JDK的安装 下载jdk1.6.0_43(这里务必要使用jdk的1.6版本,因为使用JDK1.7版本编译hadoop的时候,很多Maven依赖下载不完整,最终会报错)解压到,并将 ...
- vue路由配置,vue子路由配置
上一篇关于vue环境配置已经写好了!按照操作就行了! 现在一个项目已经部署完成,接下来我们从路由开始! 还记得在初始化项目的时候,有提示是否需要安装vue-router,对没错,vue中路由全靠它! ...
- Class.getName和Class.getCanonicalName的区别
对于一般的type来说,这二者没有区别,对于array和inner type,就有区别了,可以写代码亲测,如下: package simple; class Box { class Inner {} ...
- Zabbix添加自定义监控项(一)
前言:由于Zabbix提供的项目和模板有限,有时我们需要自定义监控项,下面以监控磁盘I/O使用率为例,创建自动发现规则,并配置图形. (1)Zabbix_agentd端自动发现脚本,zabbix要求返 ...
- SSH高级应用(端口转发)
转发自:http://www.cnblogs.com/sting2me/p/5167730.html 基于SSH协议的端口转发 [前言] 最近一直在使用ssh协议的端口转发(隧道)功能,完成对内网空透 ...