1

I am new to python but am keen to learn/optimise some basic functions which later will come in very handy for larger dataset handling. I have a series of folders where there is a list of parent folders all with the same stem but different number, and sub-folders which I want to name based off each individual parent folder.

There are parent folders for example 'Apple_1, Apple_2, Apple_3' and sub-folders for example 'red, green, pink'. I want to modify the sub-folder names to 'Apple_1 Colour_1, Apple_1 Colour_2, Apple_1 Colour_3' if this makes sense but apply a batch script to all of the parent folders within a directory.

Below is my script for applying this naming process to one single parent folder and it's sub-folders which works but how do I batch apply this to a whole series (N=50) parent folders to perform iteratively rather than individually having to run it on each parent folder?

folder path = r'D:/Home/Fruit/Apples/ParentFolder'
fileNumber = 1

for filename in os.listdir(folderPath):
    os.rename(folderPath + '//' + filename, folderPath + "//" + 
        "Parentfolder_Colour_" + str(fileNumber)) 
    fileNumber +=1

Grateful in advance for anyone's advice. Thanks

nadapez
  • 2,603
  • 2
  • 20
  • 26
Kalsi
  • 11
  • 1
  • You might try looking into Multiprocessing and see if that helps: https://docs.python.org/3/library/multiprocessing.html – whege Oct 29 '21 at 16:54
  • 1
    Thanks will do! I have an even larger dataset of 300 where I need to apply a similar process so would be great if this does it. – Kalsi Oct 29 '21 at 18:56

0 Answers0