简体   繁体   中英

Why can't my Python program find my file?

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?

Your question is about how to layout the structure of your project folder. This is a very hard topic for newbies.

Keep in mind that there are a lot of possible ways and styles to do this in Python. But looking into the setuptools documentation today the modern and most recommended style is the so called _ src -layout.

Your layout is still on its way to it and just need a bit modification.

Let's assume the name of your project is " project ". This results in the name of the repository (eg on a git server) or the project folder. It is also the name of your package . But package-folder and project-folder are different things.

Modify your layout to this

project/
    saved_data.json
    src/
        project/
            __main__.py
            __init__.py
            resources.py
            more files...

I created a new sub-folder project/src/project/ which is the package folder. The main.py file was moved into it and renamed to __main__.py (more details ). This make the folder project/src/project/ runable.

The content of __main__.py could look like this

from .ressources import load_json

if __name__ == '__main__':
   load_json()

If you are running your program (effectively) like this:

python main.py

...then...

from os.path import join

with open(join(__file__, '..', 'saved_data.json')) as file_in:
  pass # process the file here

Thus your current working directory is irrelevant because file will be the absolute path to main.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM