0

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?

2
  • Below shoule help you stackoverflow.com/questions/52485698/… Commented Jun 10, 2021 at 16:58
  • 2
    dirs.strip(root) is throwing away part of the full pathname required to locate your files. And what is the point of writing r'{}'.format(dirs) instead of just dirs? Commented Jun 10, 2021 at 21:02

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I would use glob to find the files.

import os, glob
zdir = '942ba956-8967-4bec-9540-fbd97441d17f'
files = glob.glob('*{}/000000'.format(zdir))
for fly in files:
    os.rename(fly, '{}.csv'.format(fly))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.