简体   繁体   English

ConfigObj和绝对路径

[英]ConfigObj and absolute paths

i'm having a bit of a path issue when using configobj for python. 使用configobj用于python时,我遇到了路径问题。 i'm wondering if there's a way to not use an absolute path in my helper file. 我想知道是否有一种方法可以不在我的帮助文件中使用绝对路径。 For example, instead of: 例如,代替:

self.config = ConfigObj('/home/thisuser/project/common/config.cfg')

i want to use something like: 我想使用类似:

self.config = ConfigObj(smartpath+'/project/common/config.cfg')

Background: i've placed my config file in a common directory along side a helper class and a utility class: 背景:我已将配置文件与帮助程序类和实用程序类一起放置在公共目录中:

common/config.cfg
common/helper.py
common/utility.py

the helper class has a method that returns the values within the section of the config. helper类具有一个返回配置部分中的值的方法。 the code is as such: 代码是这样的:

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

the utility file imports the helper file and the utility file is called by a bunch of different classes stationed in different folders of my project: 实用程序文件将导入帮助程序文件,并且该实用程序文件由位于项目的不同文件夹中的一堆不同的类调用:

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()

the following files import the utility file and calls the method: 以下文件将导入实用程序文件并调用该方法:

/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")

if i don't use an absolute path and i run forestCastle.py it looks for the config in /home/thisuser/project/folder1/ and i want it to look for it in project/common/ because /home/thisuser will change 如果我不使用绝对路径,而是运行forestCastle.py,它将在/ home / thisuser / project / folder1 /中查找配置,并且我希望它在project / common /中查找它,因为/ home / thisuser将更改

You can calculate a new absolute path based on a module filename instead: 您可以改为根据模块文件名计算新的绝对路径:

import os.path
from configobj import ConfigObj

BASE = os.path.dirname(os.path.abspath(__file__))


class myHelper:

    def __init__(self):
        self.config = ConfigObj(os.path.join(BASE, 'config.cfg'))

__file__ is the filename of the current module, so for helper.py that would be /home/thisuser/project/common/helper.py ; __file__当前模块的文件名,因此对于helper.py来说应该是/home/thisuser/project/common/helper.py os.path.abspath() makes sure it is a absolute path, and os.path.dirname removes the /helper.py filename to leave you with an absolute path to the 'current' directory. os.path.abspath()确保它是绝对路径,而os.path.dirname删除/helper.py文件名,为您提供到“当前”目录的绝对路径。

I'm having a bit of a hard time following what you actually want. 按照您的实际需求,我有些困难。 However, to expand the path to your home directory in an OS-agnostic fashion, you can use os.path.expanduser : 但是,要以与操作系统无关的方式扩展主目录的路径,可以使用os.path.expanduser

self.config = ConfigObj(os.path.expanduser('~/project/common/config.cfg'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM