My Flask application structure looks like
application_top/
application/
static/
english_words.txt
templates/
main.html
urls.py
views.py
runserver.py
When I run the runserver.py, it starts the server at localhost:5000.
In my views.py, I try to open the file english.txt as
f = open('/static/english.txt')
It gives error IOError: No such file or directory
How can I access this file?
I think the issue is you put
/in the path. Remove/becausestaticis at the same level asviews.py.I suggest making a
settings.pythe same level asviews.pyOr many Flask users prefer to use__init__.pybut I don’t.If this is how you would set up, try this:
Now in your views, you can simply do:
Adjust the path and level based on your requirement.