python - How to get configuration file path with configparser object -
the code below initiates conf
variable instance of configuration
class inherits configparser
. in __init__
method reads config_file
, stores configuration context in memory. how can file path config_file
having conf
object?
import configparser import os class configuration(configparser.configparser): def __init__(self, config_file): configparser.configparser.__init__(self) self.read(config_file) def savesettings(self): if not self.has_section('section a'): self.add_section('section a') self.set('section a', 'option 1', 'value 1') config_file = os.path.join(os.path.expanduser('~'),'config.ini' ) conf = configuration(config_file) conf.savesettings()
simply assign information instance (self
):
def __init__(self, config_path): configparser.configparser.__init__(self) self.read(config_path) self.config_path = config_path # instance attribute
then can access object:
conf_path = os.path.join(os.path.expanduser('~'),'config.ini' ) conf = configuration(conf_path) print( conf.conf_path ) # access via object
Comments
Post a Comment