FileNotFoundError: No such file or directory

Hello!

I started to train a deep learning module in:

“C:\Users\User\Documents\ml-basics”.

I set up a Flask in the same directory and ran it.
Everything was fine until I moved those files into a different directory:

“C:\Users\User”.

When I try to run the Flask now, all of a sudden, it gives me the Error:

“FileNotFoundError: [Errno 2] No such file or directory: ‘currency_pair_lstm_model.pth’”

The Flask.py file and the DeepLearning.ipynb file are in the same “C:\Users\User” directory.

I tried to check with the command in a .py file:

import os

file_name = ‘currency_pair_lstm_model.pth’
file_path = os.path.abspath(file_name)

print(f"The full path to the file is: {file_path}")

if os.path.exists(file_path):
print(f"The file exists at: {file_path}")
else:
print(“The file does not exist.”)

and got:

The full path to the file is: C:\Users\User\currency_pair_lstm_model.pth
The file does not exist.

What am I doing wrong`?

How come the path to the file can be found but the file itself does not exist??

Thank you!

paths do not need to exist, because you can build a path and then use it to create a file in the first place.

Where is currency_pair_lstm_model.pth? Is it in C:\Users\User\Documents\ml-basics\currency_pair_lstm_model.pth?

A .pth file must be on sys.path to have its desired effect. When running a Python script (or notebook), the ‘script directory’ (the folder containing the executed file) is added to sys.path, so if currency_pair_lstm_model.pth is in the same directory as Flask.py, it will be found. If you move Flask.py but not the .pth file, it will no longer be found unless you move them together.