2010-08-16 40 views
2

我正在開發的Python模塊有一個主配置文件/path/to/module/conf.conf/path/to/module /取決於平臺(例如,OS X中的/Users/me/module,Linux中的/home/me/module等)。定義模塊配置文件的路徑

目前我定義了/path/to/module__init__.py,在這裏我用的邏輯:

if sys.platform == 'darwin': 
ROOT = '/Users/me/module' 
elif sys.platform == 'linux': 
ROOT = '/home/me/module' 
# et cetera 

一旦我在配置文件根目錄下,我可以打開conf.conf任何地方我想要的。

你有更好的方法嗎?

回答

2

裏面你__init__.py的,你可以得到的目錄中__init_.py腳本居住使用__file__魔術變量,像這樣其中:

from os.path import dirname 
ROOT = dirname(__file__) 

那麼你知道conf.conf將位於os.path.join(ROOT, 'conf.conf')

相關問題