I am trying to write a code to rename files to numbers but i keep getting this error
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'Crypto Hood #100.jpg' -> ''
this is my code
os.chdir ('C:\\Users\\win 10\\Downloads\\Crypto Hood')
for filename in os.listdir('.'):
if filename.startswith('Crypto Hood #'):
os.rename(filename, filename[100:])
CodePudding user response:
If we take:
filename = "Crypto Hood #100.jpg"
new_filename = filename[100:]
then new_filename
would be an empty string (''
) since filename
only has 20 characters and you are slicing out the first 100. Empty strings are not valid filenames in Windows, so you get this error. I think you may be confused about how slicing works, so refer the doc: https://python-reference.readthedocs.io/en/latest/docs/brackets/slicing.html
CodePudding user response:
I suspect you wanted to do this:
os.chdir ('C:\\Users\\win 10\\Downloads\\Crypto Hood')
for filename in os.listdir('.'):
if filename.startswith('Crypto Hood #'):
newname = filename.split('#')[1]
os.rename(filename, newname)