Search code examples
pythonpython-os

Python loop through directories


I am trying to use python library os to loop through all my subdirectories in the root directory, and target specific file name and rename them. Just to make it clear this is my tree structureenter image description here

My python file is located at the root level.

What I am trying to do, is to target the directory 942ba loop through all the sub directories and locate the file 000000 and rename it to 000000.csv

the current code I have is as follow:

import os


root = '<path-to-dir>/942ba956-8967-4bec-9540-fbd97441d17f/'
for dirs, subdirs, files in os.walk(root):
    for f in files:
        print(dirs)
        if f == '000000':
            dirs = dirs.strip(root)
            f_new = f + '.csv'
            os.rename(os.path.join(r'{}'.format(dirs), f), os.path.join(r'{}'.format(dirs), f_new))

But this is not working, because when I run my code, for some reasons the code strips the date from the subduers

enter image description here

can anyone help me to understand how to solve this issue?


Solution

  • A more efficient way to iterate through the folders and only select the files you are looking for is below:

    source_folder = '<path-to-dir>/942ba956-8967-4bec-9540-fbd97441d17f/'
    files = [os.path.normpath(os.path.join(root,f)) for root,dirs,files in os.walk(source_folder) for f in files if '000000' in f and not f.endswith('.gz')]
    for file in files:
        os.rename(f, f"{f}.csv")
    

    The list comprehension stores the full path to the files you are looking for. You can change the condition inside the comprehension to anything you need. I use this code snippet a lot to find just images of certain type, or remove unwanted files from the selected files. In the for loop, files are renamed adding the .csv extension.