我在使用configobj for python时遇到了一些路径问题。我想知道是否有一种方法不使用我的帮助文件中的绝对路径。例如,而不是:ConfigObj和绝对路径
self.config = ConfigObj('/home/thisuser/project/common/config.cfg')
我想用这样的:
self.config = ConfigObj(smartpath+'/project/common/config.cfg')
背景: 我已经把我的配置文件在一个公共目录沿侧辅助类和实用程序类:
common/config.cfg
common/helper.py
common/utility.py
该帮助程序类有一个方法,该方法返回配置节中的值。代码是这样的:
from configobj import ConfigObj
class myHelper:
def __init__(self):
self.config = ConfigObj('/home/thisuser/project/common/config.cfg')
def send_minion(self, race, weapon):
minion = self.config[race][weapon]
return minion
实用程序文件导入辅助文件和程序文件是由驻扎在我的项目的不同的文件夹了一堆不同类别的所谓:
from common import myHelper
class myUtility:
def __init__(self):
self.minion = myHelper.myHelper()
def attack_with_minion(self, race, weapon)
my_minion = self.minion.send_minion(race, weapon)
#... some common code used by all
my_minion.login()
以下文件导入实用程序文件并调用方法:
/home/thisuser/project/folder1/forestCastle.py
/home/thisuser/project/folder2/secondLevel/sandCastle.py
/home/thisuser/project/folder3/somewhere/waterCastle.py
self.common.attack_with_minion("ogre", "club")
如果我不使用绝对路径和我运行forestCastle.py它看起来为配置在/HO我/ thisuser /项目/文件夹1/,我希望它看起来它在项目/普通/因为/家庭/ thisuser将改变
Arg。我正要在我的下一个编辑中提到'__file__' ... – mgilson 2013-02-25 16:13:59
太棒了!这正是我所期待的。 – aj000 2013-02-25 16:51:30