Friday 31 March 2023

Rename subfolder in multiple folders by replacing a part of the string

Assuming I have multiple subfolders inside many different folders (**). All the folders are in a main folder called Project1. I want to replace the string name of the folders using substring function.

import glob
import os 
import pandas as pd

paths = glob.glob("CC:/Users/xxx/College/Project1/**/", recursive=True)

Assuming the subfolders are in multiple folders and have a naming convention as follows:

fwjekljfwelj-10-fwefw (the path for this folder is "CC:/Users/xxx/College/Project1/**/wjekljfwelj-10-fwefw/")
kljkgpjrjrel-11-wwref
fwefjkecmuon-12-cfecd
dsfshncrpout-13-lplce

The alphanumeric sequence prior to the - character is meaningless. I want to replace the string preceding the dashed line with the number 20. The new subfolders would thus be:

2010-fwefw
2011-wwref
2012-cfecd
2013-lplce

I can do it individually for each subfolder using str.split('-', 1)[-1] and then append '20' to the name, but I would like to automate the process. I am renaming folder names, not the files themselves.



from Rename subfolder in multiple folders by replacing a part of the string

No comments:

Post a Comment