Python文件夹批处理操作代码实例
时间:2022-07-27
本文章向大家介绍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("方法1nnnn")
process1(srcpath, imgprocess_result)
print("nnn方法2")
process2(srcpath, imgprocess_result)
以上就是本文的全部内容,希望对大家的学习有所帮助。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Laravel5.1 框架Request请求操作常见用法实例分析
- 用python实现学生管理系统
- PHP CURL实现模拟登陆并上传文件操作示例
- python定义类的简单用法
- Linux yum 命令安装mysql8.0的教程详解
- 基于Python实现简单学生管理系统
- php 实现svg转化png格式的方法分析
- Python如何合并多个字典或映射
- 用Python 爬取猫眼电影数据分析《无名之辈》
- Python获取浏览器窗口句柄过程解析
- python如何支持并发方法详解
- thinkPHP+mysql+ajax实现的仿百度一下即时搜索效果详解
- php和C#的yield迭代器实现方法对比分析
- Python字符串函数strip()原理及用法详解
- python中取绝对值简单方法总结