在pythonanywhere.com上找不到文件 [英] File not found on pythonanywhere.com
问题描述
在 pythonanywhere.com 上,我有一个Django应用.这是 views.py
:
On pythonanywhere.com I have a django app. This is views.py
:
def literature(request):
module_dir = os.path.dirname(__file__)
file_path = os.path.join(module_dir, 'literature.csv')
with open(file_path, 'r') as f:
...
文件 literature.csv
与views.py位于同一目录.但是每次尝试加载页面时,都会得到 FileNotFoundError
.相同的构造在本地服务器上正常工作.错误在哪里?
File literature.csv
is located at the same directory as views.py. But every time when I try to load the page I get FileNotFoundError
. The same construction works normally on local server. Where is the bug?
推荐答案
此 os.path.dirname(__ file __)
为您提供了相对路径.在本地服务器上,恰好它对应于正确的路径.在PythonAnywhere上,使用 module_dir
的完整路径.像这样: os.path.abspath(os.path.dirname(__ file __))
This os.path.dirname(__file__)
is giving you a relative path. On your local server, it just happens that it corresponds to the correct path. On PythonAnywhere, use the full path for module_dir
. Like this: os.path.abspath(os.path.dirname(__file__))
这篇关于在pythonanywhere.com上找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!