File not found on pythonanywhere.com

一个人想着一个人 提交于 2019-12-25 04:45:19

问题


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:
        ...

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?


回答1:


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__))



来源:https://stackoverflow.com/questions/32329216/file-not-found-on-pythonanywhere-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!