import os
nameList = os.listdir()
#storing file names in nameList list
newList = []
for name in nameList:
if name.endswith(".jpg"):
newList.append(name)
# print(name)
else:
pass
#creating new list and storing only .jpg files
toChange = [5, 1, 3]
toChange.sort()
j = toChange[0]
# import pdb; pdb.set_trace()
#debugger
for name in newList:
for j in toChange:
newName = name.replace(newList[j], str(j))
os.rename(name, newName)
print(newName)
break
I want to change file names of 2nd, 4th and 6th files as denoted in toChange list. But I'm only able to change file name of 2nd file. j value is not iterating. I tried removing break but it gives me error saying cannot find specified file. Please help. Thanks