If you wanted to rename a file or change its extension by using Python, usually you would do something like:
import os old_f_name = 'a_dir/my_file.ext' new_f_name = 'a_dir/my_new_file_name.ext' new_file = os.rename(old_f_name, new_f_name)
But, if there is no file a_dir/my_file.ext, then you will get the error: FileNotFoundError: [Errno 2] No such file or directory: 'a_dir/my_file.ext' -> 'a_dir/my_new_file_name.ext'
Also, the os.rename function actually renames the file. That is, the os.rename function will rename the actually file a_dir/my_file.ext to a_dir/my_new_file_name.ext. Or, at least, will try to. This is why there is the FileNotFoundError if the file a_dir/my_file.ext does not exists.
Alternatively, you can use the pathlib library and the Path class:
from pathlib import Path
old_f_name = Path('a_dir/my_file.ext')
new_f_name = old_f_name.with_name('my_new_file_name')
new_f_name_with_new_ext = new_f_name.with_suffix('.new_ext')
print(new_f_name)
print(new_f_name_with_new_ext)
