开发者

Locking on file locations in a large Python project

When a Python project gets big, certain code segments, such as utility functions, tend to be run from various locations:

  • from a __main__
  • from a django server process
  • from a test in a test suite

In ea开发者_JAVA技巧ch 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?


using the following variable to get the directory of the project may help

 __file__ 

How to make a python program path independent?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜