Note: I’m using Windows and this program will also have to run on Mac.
I'm writing a Python app that needs to read from and write to a JSON file, which I implemented in src/resources.py
:
import json
def load_json():
with open('saved_data.json', 'rt') as file_in:
stored_text = file_in.read()
stored_json = json.loads(stored_text)
# parse stored_json & return created objects
At the beginning of the program it's supposed to load data from the file, but when I run main.py
(where I’m usually running the program from) it's giving me a runtime error:
File "<project path>\src\resources.py", line 12, in load_json
with open('saved_data.json', 'rt') as file_in:
FileNotFoundError: [Errno 2] No such file or directory: 'saved_data.json'
This is what my file structure looks like:
project/
main.py
saved_data.json
src/
__init__.py
resources.py
more files...
I've tried adding either '..\\'
or '../'
to the start of the filepath string thinking that it might be relative to resources.py instead of main.py but that resulted in an error as well. I'm not really sure what to do at this point because I don't see anywhere specific it could be going wrong. I'm pretty sure the file structure is okay. What am I missing or misunderstanding here?