I have a folder of images like this
img (1).png
img (2).png
img (3).png
I also have a list of names that I got from another folder with the same amount of files. Now I want to rename the images folder with new names using the names in the list.
import os
org = os.listdir('C:/Python310/py_scripts/new')
fake = os.listdir('C:/Python310/py_scripts/fake')
org = [i.replace('.PNG', '.png') for i in org]
for i in org:
for s in fake:
os.rename(f'C:/Python310/py_scripts/fake/{s}', f'C:/Python310/py_scripts/nw/{i}')
the images are in folder 'fake' and the list was from the names in folder 'new' and 'nw' is where I want the renamed images to be.
This is the output
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/Python310/py_scripts/fake/0001 (1).png' -> 'C:/Python310/py_scripts/nw/0008.png'
CodePudding user response:
Try this:
import os
org = os.listdir('C:/Python310/py_scripts/new')
fake = iter(os.listdir('C:/Python310/py_scripts/fake'))
org = [i.replace('.PNG', '.png') for i in org]
### If both folders contain the same amount of files.
for i in org:
os.rename(f'C:/Python310/py_scripts/fake/{next(fake)}', f'C:/Python310/py_scripts/nw/{i}')