0

I have a drive folder which contains thousands of subfolders containing hundreds of JSON files that need renaming. I am currently using a script to rename all the files in the folder according to my requirement, but I want to know if there's a way, I can automatically switch directory to another folder when the renaming is done in a folder. Here's the code I am currently using:

from google.colab import drive
drive.mount('/content/drive')


import os
import glob
import re

src = r'/content/drive/MyDrive/Training/00001/'
ext = '.json'
i = 0
for filename in glob.glob(os.path.join(src,'*' + ext)):
    if not re.search('image_\d\d\d' + re.escape(ext) +'$',filename):
        while True:
            newname = os.path.join(src,'image_{:05d}_keypoints{}'.format(i,ext))
            if os.path.exists(newname):
                i += 1
            else:
                break
        print('renaming "%s" to "%s"...' % (filename, newname))
        os.rename(filename,newname)
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
abrar
  • 19
  • 5
  • You can't really "switch directories" at all in python, at least not in the same way as you do with the `cd` command in a terminal. Instead, you keep track of a directory name or path using a value in a variable. If you want to "switch directories" in this sense, then you just assign the variable to the path you want. The key here is to accurately describe the actions you want to take. So I suggest that you first try to describe what you are trying to do in more detail. – Code-Apprentice Nov 13 '22 at 06:19
  • How does the behavior of yoru current code differ from what you actually want? – Code-Apprentice Nov 13 '22 at 06:21
  • I suggest reading the documentation for `glob.glob()`. It provides a way to recursively traverse the entire directory structure, which is what I think you are trying to do here. – Code-Apprentice Nov 13 '22 at 06:23
  • @Code-Apprentice sorry, I wasn't more detailed. My current code allows me rename contents inside a specific folder like 00001 in this case. I have thousands of other folders that have files that need to renamed, like 00002, 00003, 00004, etc., so I want to know if there's a way I can get python to run this script on all folders under the 'Training' folder. – abrar Nov 13 '22 at 06:26
  • See the link I gave above. It shows you how to do this with a small change to how you use `glob.glob()`. You should also read the glob documentation to better understand how it works. – Code-Apprentice Nov 13 '22 at 06:28
  • @Code-Apprentice hi there, I added the option to traverse and recursive=true, but it's not saving the renamed files according to the folders, instead saving all of them in the main folder that contains all the subfolders. Could you please advice please? Thanks. – abrar Nov 13 '22 at 12:38
  • @Code-Apprentice also, hey, I just noticed the renaming is being done randomly. Is it possible to rename them according to name in ascending order, so that the sequence is maintained? – abrar Nov 13 '22 at 15:12
  • "instead saving all of them in the main folder that contains all the subfolders." Look closely at how you create `newname`. It only uses the main folder. You will need to change that to use the subfolders. – Code-Apprentice Nov 13 '22 at 15:54
  • p.s. You should leave your original question as it was and post a new question with the new version of your code for what you are trying to solve next. – Code-Apprentice Nov 13 '22 at 15:54
  • @Code-Apprentice noted, I will do that next time. Could you please provide an answer on how to I can dynamically set the directory when creating newname? As for the renaming, is it possible to do it sequentially according to the name? Sorry, the discussion is getting rather long for comments and I am a total newbie – abrar Nov 13 '22 at 16:53
  • You should post two new questions for what you need help with. I am rolling back the changes to your code so that the comments make sense. – Code-Apprentice Nov 13 '22 at 18:08

0 Answers0