Rename certain subfolders in a directory using python

Viewed 52

I have a folder structure that looks something like this:

/Forecasting/as_of_date=20220201/type=full/export_country=Spain/import_country=France/000.parquet'

and there are approx 2500 such structures.

I am trying to rename only certain subfolders, mainly export_country as exp_cty and import_country as imp_cty.

So far I tried this but it doesn't seem to work. I never had to deal with such complex folder structures and I'm a bit unsure how to go about it. My script is below:

import os
from pathlib import Path
path = r"/datasets/local/Trade_Data"       # Path to directory that will be searched

old = "import_country*"
new = "imp_cty"
for root, dirs, files in os.walk(path, topdown=False):      # os.walk will return all files and folders in thedirectory
    for name in dirs:                                       #  only concerned with dirs since I only want to change subfolders
        directoryPath = os.path.join(root, name)            # create a path to subfolder
        if old in directoryPath:                            # if the 'export_country' is found in my path then
            parentDirectory = Path(directoryPath).parent    # save the parent directory path
            os.chdir(parentDirectory)                       # set parent to working directory
            os.rename(old, new)   
1 Answers

The code you have proposed has 2 issues:

The first one: if old in directoryPath: checks if the string import_country* is inside the path.

From your question I have understood that you would like to rename all directories that start with "import_country" so you can use the startswith for that.

The second problem is os.rename(old, new) you are trying to rename directory with name import_country* which doesn't exist, instead you should use the name variable.

Here is your code with slightly changes that is working, please note that you must use topdown=False as you are renaming directories while walking through them:

import os
from pathlib import Path

path = "/datasets/local/Trade_Data"
old_prefix = "import_country"
new_name = "imp_cty"
for root, dirs, files in os.walk(path, topdown=False):
    for name in dirs:
        if name.startswith(old_prefix):
            directoryPath = os.path.join(root, name)
            parentDirectory = Path(directoryPath).parent
            os.chdir(parentDirectory)
            os.rename(name, new_name)
Related