SoFunction
Updated on 2024-10-30

How to batch process nested folders via Python

preamble

When I was pre-processing some of the training images for the project team, I found that the processed images were categorized and stored in folders within folders within folders, nested, so it caused a lot of trouble for me to batch process them, as well as store them according to the original folder rules
But the combination of the following functions helped me to smoothly complete a series of preprocessing.

I. Zhi Shan Ku that you can't get tired of using

1. Installation of libraries

pip install:

pip install zisan

function (math.)

function call:

import  as zf
file_path = 'C:/Users/xxx/Desktop/2016/Annotations'
whole_file = (file_path) 

Images are stored in : folder 2016 -> folder Annotations -> subfolders ->.
With the getFiles function, you can pull out the paths of all the images in all the folders in Annotations

That is, the getFiles function pulls out the paths of all the files in the folder, regardless of whether there are subfolders in between.

II. Other functions

function (math.)

This function is called to return the name of the folder under the path, stored as a string in the list

The code is as follows:

import os
file_path = 'C:/Users/xxx/Desktop/2016/Annotations'
file_names = (file_path)
print(file_names)

Effect:


function (math.)

Code:

import os
new_file_path = 'C:/Users/xxx/Destop/2016/newfile'
(new_file_path)

For creating new folders

III. Utilization

Requirement: Process the images in each subfolder of the Annotations folder and store them in the new folder of the newfile according to the original rules.

import  as zf
import os
import cv2
from skimage import io

file_path = 'C:/Users/xxx/Desktop/2016/Annotations'
new_file_path = 'C:/Users/xxx/Destop/2016/newfile'

file_names = (file_path)
# Get subfolder names of the Annotations folder

for i in file_names: # Iterate over each subfolder name
  Index = 0
  file_name = file_path + '/' + i #Cleverly use the + sign to get to change the path of a subfolder
  (new_file_path + '/' + i) # Create a folder with the same name as the subfolder in newfile
  whole_pic = (file_name) # Use the getFiles function to read the paths of images in subfolders.
  for f in whole_pic:
    msk = (f)
		msk=(msk,cv2.COLOR_RGBA2GRAY)
    msk[(msk!=0)]=255
    (new_file_path + '/' + i + '/' + str("%05d" % Index) + '.jpg' , msk)
     #Process naming can be directly + '.jpg' to make it stored as jepg
    Index += 1

This is my basic idea and process to solve the problem of folder processing, each function can be used together, into the loop outside or inside depending on the specific requirements have different effects.

to this article on how to batch process the folder through Python batch processing to this article, more related Python batch processing file content, please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!