[英]Why can't my Python program find my file?
Note: I'm using Windows and this program will also have to run on Mac.注意:我使用的是 Windows,这个程序也必须在 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
:我正在编写一个需要读取和写入 JSON 文件的 Python 应用程序,我在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:在程序开始时,它应该从文件中加载数据,但是当我运行main.py
(我通常从中运行程序)时,它给了我一个运行时错误:
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.我尝试将'..\\'
或'../'
添加到文件路径字符串的开头,认为它可能与 resources.py 而不是 main.py 相关,但这也会导致错误。 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.请记住,在 Python 中有很多可能的方法和样式可以做到这一点。 But looking into the setuptools
documentation today the modern and most recommended style is the so called _ src
-layout.但是今天查看setuptools
文档,现代且最推荐的样式是所谓的 _ 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
".假设您的项目名称是“ project
”。 This results in the name of the repository (eg on a git server) or the project folder.这会产生存储库的名称(例如,在 git 服务器上)或项目文件夹的名称。 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.我创建了一个新的子文件夹project/src/project/
这是包文件夹。 The main.py
file was moved into it and renamed to __main__.py
(more details ). main.py
文件被移入其中并重命名为__main__.py
(更多细节)。 This make the folder project/src/project/
runable.这使文件夹project/src/project/
可运行。
The content of __main__.py
could look like this __main__.py
的内容可能如下所示
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因此,您当前的工作目录无关紧要,因为file将是 main.py 的绝对路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.