Open file in Django app Open file in Django app django django

Open file in Django app


The solution has been described in the Favorite Django Tips&Tricks question. The solution is as follows:

import osmodule_dir = os.path.dirname(__file__)  # get current directoryfile_path = os.path.join(module_dir, 'baz.txt')

Which does exactly what you mentioned.

Ps. Please do not overwrite file variable, it is one of the builtins.


I think I found the answer through another stack overflow question (yes, I did search before asking...)

I now do this

pwd = os.path.dirname(__file__)file = open(pwd + '/baz.txt')