I have files in a directory and the filenames are a substring of a list of strings. I need to rename the files with the strings in the list
filenames in "./temp" = aa.txt, bb.txt, cc.txt
list_of_names = ['aa12.txt', 'bb12.txt', 'cc12.txt']
I want the files to be renamed to those in the list_of_names
. Tried the code below but get an error
for filename in os.listdir('./temp'):
for i, item in enumerate(list_of_names):
if filename in item:
os.rename(filename,list_of_names[I])
FileNotFoundError: [Errno 2] No such file or directory: 'aa.txt' -> 'aa12.txt'