Rename files with Python using os.rename

less than 1 minute read

import os
 
def changeName(path, cName):
    i = 1
    for filename in os.listdir(path):
        print(path+filename, '=>', path+str(cName)+str(i)+'.jpg')
        os.rename(path+filename, path+str(cName)+str(i).zfill(6)+'.jpg')
        i += 1

changeName('real_images/','')

Reference

https://data-make.tistory.com/171

Leave a comment