I'm starting learning Python, I'm doing a project that renames a bunch of files from a list of the text files that contains the new names of the files.
In my folder I have files like lesson1.mp4, lesson2.mp4, lesson3.mp4 ... and so on. And in the text file, I have the new names 1-newname, 2-newname, 3-newname (separated by a newline).
First, I change the directory and store the variable in file and then I use file.readlines() to separate line by line.
import os
os.chdir(r"C:\Users\Samaritan\Documents\Python\
Ejercicios\pruebarenamefiles")
file = open('new_names.txt')
lines = file.readlines()
Then, I created a function to return the new names based on the position of the list.
def n_name(num):
return lines[num]
Then I iterate over the directory and rename each position with the function n_name
i = 0
for file in os.listdir():
if file != "new_names.txt":
os.rename(file, n_name(i))
i += 1
But it doesn't work at all. I got "OSError: [WinError 123]"
C:\Users\Samaritan\Documents\Python\Ejercicios\rename_files\venv\Scripts\python.exe C:/Users/Samaritan/Documents/Python/Ejercicios/rename_files/main.py
Traceback (most recent call last):
File "C:\Users\Samaritan\Documents\Python\Ejercicios\rename_files\main.py", line 16, in <module>
os.rename(file, n_name(i)) OSError: [WinError 123] El nombre de archivo, el nombre de directorio o la sintaxis de la etiqueta del volumen no son correctos: 'Nuevo documento de texto - copia (2).txt'
-> '01 - Course Introduction\n'
Process finished with exit code 1
In English it means something like this: WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: