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)