Reputation: 107092
When a Python project gets big, certain code segments, such as utility functions, tend to be run from various locations:
__main__
In each case the working directory for the python interpreter may be different and assuming the project spans over a sub-directory tree, the following line doesn't always work:
with open('some_file.xml','r') as my_xml:
It doesn't work because some_file.xml
isn't always in your working directory. You need to be specific regarding the file's location, however, the project may be deployed in various environments so simply adding the directory to the open
statement isn't a good solution.
What would be an elegant and efficient way to "lock on" the location of the file throughout the project?
Upvotes: 0
Views: 82
Reputation: 4764
using the following variable to get the directory of the project may help
__file__
How to make a python program path independent?
Upvotes: 1