Python文件夾批處理操作代碼實(shí)例
如圖所示,有一個(gè)test文件夾,里面有3個(gè)子文件夾,每個(gè)子文件夾中有若干圖片文件


#場(chǎng)景1 讀取一個(gè)文件夾中所有文件,存入到一個(gè)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
#場(chǎng)景1 讀取一個(gè)文件夾中所有文件,存入到一個(gè)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)
#是否隨機(jī)打亂文件順序
#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)
#開(kāi)始處理文件
#..............
#
#
#場(chǎng)景2 首先讀取一個(gè)文件夾中的所有子目錄,然后依次遍歷各個(gè)子目錄的所有文件
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)
#開(kāi)始處理文件
#..............
#
#
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)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)隨機(jī)游走的示例代碼
隨機(jī)游走是一個(gè)數(shù)學(xué)對(duì)象,稱為隨機(jī)或隨機(jī)過(guò)程,它描述了一條路徑,該路徑由一些數(shù)學(xué)空間上的一系列隨機(jī)步驟組成,下面我們就來(lái)學(xué)習(xí)一下Python如何實(shí)現(xiàn)隨機(jī)游走的吧2023-12-12
Python使用matplotlib繪圖無(wú)法顯示中文問(wèn)題的解決方法
這篇文章主要介紹了Python使用matplotlib繪圖無(wú)法顯示中文問(wèn)題的解決方法,結(jié)合具體實(shí)例形式分析了Python使用matplotlib繪圖時(shí)出現(xiàn)中文亂碼的原因與相關(guān)解決方法,需要的朋友可以參考下2018-03-03
python通過(guò)matplotlib生成復(fù)合餅圖
這篇文章主要介紹了python通過(guò)matplotlib生成復(fù)合餅圖,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
python實(shí)現(xiàn)五子棋游戲(pygame版)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)五子棋游戲,pygame版五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
Python plt.imshow函數(shù)及其參數(shù)使用
plt.imshow()是Matplotlib庫(kù)中的一個(gè)函數(shù),主要用于顯示圖像或矩陣數(shù)據(jù),本文主要介紹了Python plt.imshow函數(shù)及其參數(shù)使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Python爬蟲(chóng)獲取圖片并下載保存至本地的實(shí)例
今天小編就為大家分享一篇Python爬蟲(chóng)獲取圖片并下載保存至本地的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

