Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python configparser writing to defaults section

i'm using the configparser module in python to read and write some .ini style files. i want to be able to create and write to the DEFAULTS section, however, it appears to be hardcoded to not allow the creation of such a section.

is it possible? or even advised to do this?

like image 537
yee379 Avatar asked Oct 17 '11 23:10

yee379


1 Answers

You don't have to create the DEFAULT section, it already exists. You can set values in it right away.

config = ConfigParser.RawConfigParser()
config.set('DEFAULT', 'name2', 'value2')
with open('file.conf', 'wb') as cf:
    config.write(cf)

The values you set as defaults when creating the ConfigParser instance will also get written to the DEFAULT section, as wim noted.

like image 68
Beli Avatar answered Oct 26 '22 21:10

Beli