I am using ConfigParser
to read the runtime configuration of a script.
I would like to have the flexibility of not providing a section name (there are s
Enlightened by this answer by jterrace, I come up with this solution:
ini_str = '[root]\n' + open(ini_path, 'r').read()
ini_fp = StringIO.StringIO(ini_str)
config = ConfigParser.RawConfigParser()
config.readfp(ini_fp)
EDIT for future googlers: As of Python 3.4+ readfp
is deprecated, and StringIO
is not needed anymore. Instead we can use read_string
directly:
with open('config_file') as f:
file_content = '[dummy_section]\n' + f.read()
config_parser = ConfigParser.RawConfigParser()
config_parser.read_string(file_content)