0

My application was working fine until yesterday - it's nearly finished and is due tonight. Yesterday for some reason it failed to recognise any of the files within the app folder (I am using VSC). I have no idea why this suddenly occurred, but I have checked the directory of the app.py file and it is the same path as my additional files. I even tried loading my project on a different pc incase the problem was with my internal file system, but got the same error.

The error printed below is for the first file my app calls, datab.yaml, but when I remove this I get the same error with the next file I try to call (sentiment_model.pkl), and so on.

Error:

File "c:\Users\jeram\Documents\NCI\Semester3\TriagatronX50\app.py", line 57, in <module>
    db = yaml.safe_load(open('datab.yaml'))
FileNotFoundError: [Errno 2] No such file or directory: 'datab.yaml'

I have tried

import os.path
prog = ("/Users/jeram/Documents/NCI/Semester3/TriagatronX50")
directory = os.path.dirname(os.path.abspath(prog))
print(os.path.join(directory, 'datab.yaml'))

But it still throws the same error when I run it.

Any help very much appreciated!!

davidism
  • 121,510
  • 29
  • 395
  • 339
LilRi
  • 49
  • 1
  • 6
  • You can try to get the current working directory (where that yaml file is) using some OS library functionality and get its absolute path and then try opening the file. BTW, how are you running this? What command and from which directory? – kiner_shah Jan 03 '22 at 12:22
  • I was running it on my main app.py file using the control panel in VSC ('run without debugging'), but after your comment I instead tried to run it using the 'flask run' command in the terminal, and it worked?! So bizarre since I was using the other button up until now. Thank you so much! For future reference though, how can I check the absolute path of the yaml file when I can only run os commands in a python file? I'm pretty new to this so don't completely understand how the directory system works. – LilRi Jan 03 '22 at 12:37
  • You can use [getcwd](https://docs.python.org/3/library/os.html#os.getcwd) to get the current working directory (CWD). If your CWD is not the one containing yaml file, you will know it that way. – kiner_shah Jan 03 '22 at 12:41

1 Answers1

0

the problem is: the directory of python file when executing is not the directory you put datab.yaml. so, you should designate the whole abstract path of datab.yaml like the following:

import os.path
prog = ("/Users/jeram/Documents/NCI/Semester3/TriagatronX50")
directory = os.path.dirname(os.path.abspath(prog))
print(os.path.join(directory, 'datab.yaml'))

db = yaml.safe_load(open(os.path.join(directory, 'datab.yaml')))

and then execute it.

dongrixinyu
  • 172
  • 2
  • 14