Skip to content
Advertisement

How to rename subfolders by numerical pattern and order based on theirs parent in python

I have a folder with many sub-folders, and each sub-folder also has two sub-folders. The structure looks like this:

JavaScript

What I want to achieve is extract the folder with smaller number in name(in the above example, 1-1000, 1-1030 and 1-1040) and rename these folders according to their parent folders(sub-f1-1, sub-f2-1 and sub-f3-1). I’m running Windows 10 and any simple solutions are welcome!

Advertisement

Answer

I have written a fairly verbose algorithm to explain step by step how it works:

JavaScript

output will be:

JavaScript

This solution can be implemented in many other ways, even without using glob and just using os.walk() for example. You can use much more optimized algorithms but it depends on how many folders you need to rename. This is just a simple example.

Advertisement