Python文件夹批处理操作代码实例
作者:Maddock 时间:2022-12-15 12:58:55
如图所示,有一个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)
来源:https://www.cnblogs.com/adong7639/p/8527161.html
标签:python,文件夹,批,处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
举例讲解Django中数据模型访问外键值的方法
2022-05-29 18:40:11
最新Linux系统下安装MySql 5.7.17全过程及注意事项
2024-01-17 07:40:56
怎么让别人看不到网页源代码
2008-03-21 13:06:00
如何解决AJAX中跨域访问出现'没有权限'的错误
2008-09-11 17:20:00
python爬虫爬取淘宝商品信息(selenum+phontomjs)
2022-07-07 05:40:52
![](https://img.aspxhome.com/file/2023/9/72869_0s.jpg)
python人物视频背景替换实现虚拟空间穿梭
2023-06-11 06:58:30
![](https://img.aspxhome.com/file/2023/0/82690_0s.png)
一场关于YUI3/jQuery的精彩辩论
2010-11-11 12:50:00
关于微信小程序使用echarts/数据刷新重新渲染/图层遮挡问题
2024-05-22 10:39:44
![](https://img.aspxhome.com/file/2023/7/123407_0s.png)
Python实现多进程的四种方式
2022-03-14 10:48:46
gin项目部署到服务器并后台启动的步骤
2022-03-25 09:30:23
关于MySQL 优化的100个的建议
2024-01-19 12:31:39
![](https://img.aspxhome.com/file/2023/7/129887_0s.png)
Python开根号的几种方式详解
2021-07-10 20:37:34
![](https://img.aspxhome.com/file/2023/5/75935_0s.png)
配置node服务器并且链接微信公众号接口配置步骤详解
2024-05-03 15:54:12
如何判断URL格式是否符合规范?
2010-01-12 20:14:00
pandas的apply函数用法详解
2022-05-09 11:41:16
详解Python GUI工具取色器
2021-01-23 10:12:04
![](https://img.aspxhome.com/file/2023/4/87764_0s.gif)
Python 注解方式实现缓存数据详解
2023-07-15 22:11:22
JavaScript基于ChatGPT API实现划词翻译浏览器脚本
2024-04-22 12:52:27
![](https://img.aspxhome.com/file/2023/7/136017_0s.png)
MySQL中由load data语句引起死锁的解决案例
2024-01-19 19:37:14
![](https://img.aspxhome.com/file/2023/6/121586_0s.png)
使用python获取CPU和内存信息的思路与实现(linux系统)
2023-06-11 04:17:47