SoFunction
Updated on 2024-11-18

Python folder batch operation code example

As shown in the figure, there is a test folder with 3 subfolders, each of which contains several image files

#Scenario 1: Read all the files in a folder and store them in a list table.

#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 = (path) 
  for filename in filelist: 
    filepath = (path, filename) 
    if (filepath):
      #print(filepath) 
      findAllfile(filepath, allfile) 
    else: 
      (filepath) 
  return allfile 

#Scenario 1: Read all the files in a folder and store them in a list table.
def process1(srcpath, imgprocess_result):

  # Iterate over image folders
  image_files = findAllfile(srcpath,[])
  #image_files as a list of all files

  # Determine if the directory exists, delete it if it does, and rebuild it.
  if (imgprocess_result):
    ("rm -rf " + imgprocess_result)
  if not (imgprocess_result): # Create the log directory if it doesn't exist
    (imgprocess_result)

  # Whether to randomize the order of files
  #(image_files)

  # Iterate over all
  for facepath in image_files:
    print("Original file path:", facepath)
    # Get the name of the document
    data_split = ().split("/")
    image_floder = data_split[-2]
    print("File directory:", image_floder)
    image_name = data_split[-1]
    print("File name:", image_name)

    image_newfloder = imgprocess_result + "/" + image_floder
    # Determine if the directory exists, rebuild if it does not exist
    if not (image_newfloder): # Create the log directory if it doesn't exist
      (image_newfloder)

    image_newpath = image_newfloder + "/" + image_name
    print("Processed file path:", image_newpath)

    # Start processing documents
    #..............
    #
    #


#Scenario 2: First read all the subdirectories in a folder, then iterate through all the files in each subdirectory in turn.
def process2(srcpath, imgprocess_result):
  # Find all subdirectories
  filelist = (srcpath) 
  for filename in filelist: 
    filepath = (srcpath, filename) 
    if (filepath):
      print("Original subdirectory path:", filepath)
      image_files = findAllfile(filepath,[])
      for facepath in image_files:
        print("Original file path:", facepath)
        # Get the name of the document
        data_split = ().split("/")
        image_floder = data_split[-2]
        print("File directory:", image_floder)
        image_name = data_split[-1]
        print("File name:", image_name)

        image_newfloder = imgprocess_result + "/" + image_floder
        # Determine if the directory exists, rebuild if it does not exist
        if not (image_newfloder): # Create the log directory if it doesn't exist
          (image_newfloder)

        image_newpath = image_newfloder + "/" + image_name
        print("Processed file path:", image_newpath)

        # Start processing documents
        #..............
        #
        #

if __name__ == '__main__':
  # Original folder
  srcpath = "/DATA/share/publicdata/face/dmt_face/test"
  # Store files after processing
  imgprocess_result = "/DATA/share/publicdata/face/dmt_face/imgprocess_result"

  print("Method 1\n\n\n\n")
  process1(srcpath, imgprocess_result)
  print("\n\n\n\n Method 2")
  process2(srcpath, imgprocess_result)

This is the whole content of this article.