How to rename multiple files or data sets which is in Google drive using Python ?

GC JYOTHI PRASANNA
My World with Python
1 min readJul 8, 2019

Renaming the multiple files is a difficult task .It is simply achieved using the rename() and listdir() methods in os module.You can also find a code here.

Let we achieve this task within 4 steps.

Step 1: Open Google Colaboratory .Import drive and files in a google colab by using below commands. Here I want to rename files which is in data_label folder.So, I had given a path in data variable.

from google.colab import drive
from google.colab import files
drive.mount(‘/content/drive’)

data = “/content/drive/My Drive/data_label/”

Step 2: Here listdir() function is used to list out all the content in a given directory.

import os
ldseg=np.array(os.listdir(data))

Step 3: Now, define a function to rename multiple files . Here I used split function to divide the name of a file into 2 keywords.And I am renaming the file with first keyword with .png extension.

#Function to rename multiple files
def main():
i = 0
for filename in ldseg:
dst =filename.split(‘_’)[0] + “.png”
src =data+ filename
dst =data+ dst
os.rename(src, dst)
i += 1

Step 4: Call the main function .

if __name__ == ‘__main__’:
main()

--

--