Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
727 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...