Dylan Grove Dylan Grove - 3 years ago 144
Python Question

Python file renaming script

I'm trying to remove "(Lyrics _ Lyric Video) " from the my album of 60 songs.

import os

toBeRemoved="(Lyrics _ Lyric Video) "
path=r'E:\Users\Dylan\Desktop\MediaHuman2'
directory = os.listdir(os.fsencode(path))

for file in directory:
filename = os.fsdecode(file)
if toBeRemoved in filename:
newname=filename.replace(toBeRemoved, "")
os.rename(path+filename, path+newname)
else:
continue


but I keep getting this:

Traceback (most recent call last):
File "E:\Users\Dylan\Desktop\filerenamer.py", line 11, in <module>
os.rename(path+filename, path+newname)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'E:\\Users\\Dylan\\Desktop\\MediaHuman202 - NOTD - Summer of Love (Lyrics _ Lyric Video) Ft. Dagny.mp3' -> 'E:\\Users\\Dylan\\Desktop\\MediaHuman202 - NOTD - Summer of Love Ft. Dagny.mp3'


EDIT: the file does exist

Answer Source

Try with

path=r'E:\Users\Dylan\Desktop\MediaHuman2''\\'

instead of

path=r'E:\Users\Dylan\Desktop\MediaHuman2'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download