Reputation: 13
I'm trying to rename multiple files from a folder so that every alphabet is removed but i get this error when I run it:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Amsterdam1971' -> '1971'
import os
os.chdir(directory)
for f in os.listdir():
f_name, f_ext = os.path.splitext(f)
os.rename(f_name,f_name.translate(str.maketrans("","","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")))
Upvotes: 1
Views: 5482
Reputation: 5713
You are facing this issue since you are trying to update the file name without the extension. You should add the file extension to fix this issue.
import os
os.chdir(directory)
for f in os.listdir():
f_name, f_ext = os.path.splitext(f)
os.rename(f_name+'.'+f_ext,f_name.translate(str.maketrans("","","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"))+'.'+f_ext)
Upvotes: 1