-2

I have a folder with about 1000 images ordered from 1.jpg to 1000.jpg, I want to rename these names with a list "x" that I have with me.

x = ["hello", "rat", ...]

This is the list that I have, so I would like to change the names of the images from 1.jpg to hello.jpg and so on. How do I do this?

I thought of reading the file and then using os.rename(), but I dint know how to do it

with open(x) as list1:
newnames = list1.read().split(',\n')
Sahil
  • 439
  • 2
  • 6
  • 17

1 Answers1

-1
import os
my_list_of_new_names = ["hello", "rat", "etc..."]
my_dir = "C:\path_to_my_images\my_directory_of_images"
for filename in os.listdir(my_dir):
    filename = filename.split('.')
    try:
        new_name, ext = filename
        os.rename(filename, my_list[int(new_name)-1] + ext)
    except:
        #your list may not be the correct size
        pass
TheLazyScripter
  • 2,541
  • 1
  • 10
  • 19