Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigParser and 2 config files

Tags:

python

I want to split my config file to 2 config files: common.conf and particular.conf. It's possible parse 2 config files as one config ?

like image 708
Bdfy Avatar asked Feb 23 '23 08:02

Bdfy


1 Answers

Just pass all the filenames to configparser. It's in the docs:

Attempt to read and parse a list of filenames, returning a list of filenames which were successfully parsed. If filenames is a string or Unicode string, it is treated as a single filename. If a file named in filenames cannot be opened, that file will be ignored. This is designed so that you can specify a list of potential configuration file locations (for example, the current directory, the user’s home directory, and some system-wide directory), and all existing configuration files in the list will be read. If none of the named files exist, the ConfigParser instance will contain an empty dataset. An application which requires initial values to be loaded from a file should load the required file or files using readfp() before calling read() for any optional files:

import ConfigParser, os

config = ConfigParser.ConfigParser()
config.readfp(open('defaults.cfg'))
config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])

Changed in version 2.4: Returns list of successfully parsed filenames.

like image 159
Katriel Avatar answered Mar 05 '23 03:03

Katriel