why im getting FileNotFoundError: [WinError 2] The system cannot find the file specified: error

1.1k views Asked by At

Here I'm trying to rename the files bbut idont know what is wrong i'm getting FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Screenshot (72).png' -> '72-Screenshot.png' plz anyone show me the path to resolve this error

import os
for i in os.listdir('Experiment'):
    f_name, f_ext = os.path.splitext(i)
    f_name, f_num = f_name.split('(')
    f_name = f_name.strip()
    f_num, f_useless = f_num.split(')')
    k = f'{f_num}-{f_name}{f_ext}'
    os.rename(i, k)

[[1]: https://i.stack.imgur.com/XYpdL.png[1]

2

There are 2 answers

1
Arun Kumar On BEST ANSWER

[WinError 2] The system cannot find the file specified: 'Screenshot (72).png

-----------> remove the space form the <file_name>.png Program :

import os
path  = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
     os.rename(os.path.join(path, filename), os.path.join(path, filename.replace(' ', '-')))
1
Mina Abd El-Massih On

What you're missing is adding the 'Experiment/' directory before the ith file in that directory it should be like os.path.splitext('Experiment/' + i).

Instead what's happening is you're looking for the ith file in the python file directory instead of the 'Experiment' directory.

Same thing with the rename the 'Experiment' directory should be included before the i and the k as well.