I have few excel files in a folder which I need to rename. I have also created a list that contains new name for each file. The new name should be appended at the end of each file.
I tried,
import os
path = 'C:/Users/myname/Desktop/SM'
files = sorted(os.listdir(path))
wo = ['1','2','3','4','5','6','7','8','9','10'] # Text to be added at the end of the file
for index, file in enumerate(files):
os.rename(os.path.join(path, file), os.path.join(path, ''.join([files[index].split('.xlsx')[0] + '-' + wo[index], '.xlsx'])))
This code renames the file but not the correct file to be renamed. The first value in the list should be used to rename the first file in the folder. But here after reading the files, the order is changing. Is there any other ways to rename files without changing order as it is in folder?