6

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?

yee379
  • 6,498
  • 10
  • 56
  • 101
  • whether it's advisable depends on the target of the ini file. configparser can parse it just fine, so if thats its only use, it will work fine. other programs may or may not make sense of it. – SingleNegationElimination Oct 18 '11 at 01:34

1 Answers1

5

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.

Beli
  • 594
  • 5
  • 16