日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python批处理文件_Python文件夹批处理操作代码实例

發布時間:2024/1/23 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python批处理文件_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文件夹批处理操作代码实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。