Write to lowercase 'default' section ConfigParser

Question:

I am working with an ini file where the default section is named ‘default’ and not ‘DEFAULT’. Pythons ConfigParser seams to handle these sections ok with no problem. However, for whatever reason, they dont allow you to add a ‘default’ section or write to it.
In the code, they have hardcoded that you can not create a section of any case-insensitive version of ‘DEFAULT’.

So if I can not add the section ‘default’ how do I write to it? The spec says you write to ‘DEFAULT’ since the section always exists. However, how to I write to ‘default’?

I want to stay consistent with the way the file is already written, so I want the default section to be written in lowercase and not all uppercase.

If the file doesnt exist yet, I want to add the [default] section. Writing to section ‘default’ gives me a ValueError: Invalid section name: default

(note: writing to default works fine when I use an already correctly formatted file)

Also, I am willing to listen to suggestions for other configuration libraries I can use. But they must not be 3rd party (I shouldn’t need to pip install them)

Asked By: Nick Humrich

||

Answers:

Here’s a workaround (setting ConfigParser.DEFAULTSECT temporarily) :

import ConfigParser
import sys

config = ConfigParser.ConfigParser()
config.set(ConfigParser.DEFAULTSECT, 'name', 'value')

ORIG_DEFAULTSECT = ConfigParser.DEFAULTSECT # <---
ConfigParser.DEFAULTSECT = 'default'
try:
    config.write(sys.stdout)
finally:
    ConfigParser.DEFAULTSECT = ORIG_DEFAULTSECT # <---

Alternative: pass a StringIO with desired section name to readfp

import ConfigParser
import sys
from StringIO import StringIO

config = ConfigParser.ConfigParser()
config.readfp(StringIO('[default]'))  # <----
config.set('default', 'name', 'value')
config.write(sys.stdout)
Answered By: falsetru

Update for 3.7

I don’t know if this has always been the case but I’m currently dealing with configparser in 3.7 and came across this post. The easiest way to get this done as of this writing is to just set your default section name during the __init__ of configparser. Pay close attention to the character case in the examples below.

Example:

config = configparser.ConfigParser(default_section='default')

Then you can simply add to the default section with:

config.set('default', 'option', 'value')

or

config['default'] = {'option1':'value1', 'option2':'value2'}

Note:
The second method above (the dict-type method) will overwrite anything that already exists in the section, including the section name case if you’re not careful. Be consistent on your naming convention to avoid this.

Answered By: dsanchez

I think that the simplest approach is this…

from configparser import ConfigParser

config_parser = ConfigParser()
config_parser.default_section = "default"
Answered By: BoĊĦtjan Mejak
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.