Search code examples
pythonmodulepycharmoperating-systemfile-not-found

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


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.sstatic.net/XYpdL.png[1]


Solution

    [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(' ', '-')))