Python Forum
ConfigParser.NoSectionError: No section: 'Default'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ConfigParser.NoSectionError: No section: 'Default'
#1
I'm stuck. If I try to add an option to section Default I get the below error. But I also get an error when I try to first add section Default. Please advise.

#!/usr/bin/env python
import ConfigParser
config = ConfigParser.ConfigParser()
section = 'Default'
option = 'foo'
value = 'bar'
config.set(section, option, value)
Output:
... raise NoSectionError(section) ConfigParser.NoSectionError: No section: 'Default'
Reply
#2
Try code like the following. Please note that you are using Python 2.

For Python 3 'ConfigParser.' becomes lower case 'configparser.' (configparser dot) and import ConfigParser becomes import configparser

'Default' must be 'DEFAULT' (all caps)
You can also use ConfigParser.DEFAULTSECT

Python 2 code follows:
# Reference: https://python-forum.io/Thread-ConfigParser-NoSectionError-No-section-Default
# Reference Python 2: https://docs.python.org/2/library/configparser.html
# Reference Python 3: https://docs.python.org/3/library/configparser.html

# The following is Python 2 code
import ConfigParser

config = ConfigParser.ConfigParser()

section = "DEFAULT"
option = 'foo'
value = 'bar'
config.set(section, option, value)

section = ConfigParser.DEFAULTSECT
option = 'foo2'
value = 'bar2'
config.set(section, option, option)

section = "DEFAULT"
foo_value = config.get(section,'foo')
foo_value = config.get(section,'foo')
print("section {} foo_value = {}".format(section, foo_value))

foo2_value = config.get(section,'foo2')
print("section {} foo2_value = {}".format(section, foo2_value))



section = 'xyz'
option = 'abc'
value = 'abcbar'
config.add_section(section)
config.set(section, option, value)

option = 'abc2'
value = 'abcbar2'
config.set(section, option, value)

foo_value = config.get(section,'abc')
print("section {} foo_value = {}".format(section, foo_value))

foo2_value = config.get(section,'abc2')
print("section {} foo2_value = {}".format(section, foo2_value))

print("Done")
Output:
section DEFAULT foo_value = bar section DEFAULT foo2_value = foo2 section xyz foo_value = abcbar section xyz foo2_value = abcbar2 Done
Python 3 code follows (output is identical):
# Reference: https://python-forum.io/Thread-ConfigParser-NoSectionError-No-section-Default
# Reference Python 2: https://docs.python.org/2/library/configparser.html
# Reference Python 3: https://docs.python.org/3/library/configparser.html

# The following is Python 2 code
import configparser

config = configparser.ConfigParser()

section = "DEFAULT"
option = 'foo'
value = 'bar'
config.set(section, option, value)

section = configparser.DEFAULTSECT
option = 'foo2'
value = 'bar2'
config.set(section, option, option)

section = "DEFAULT"
foo_value = config.get(section,'foo')
foo_value = config.get(section,'foo')
print("section {} foo_value = {}".format(section, foo_value))

foo2_value = config.get(section,'foo2')
print("section {} foo2_value = {}".format(section, foo2_value))



section = 'xyz'
option = 'abc'
value = 'abcbar'
config.add_section(section)
config.set(section, option, value)

option = 'abc2'
value = 'abcbar2'
config.set(section, option, value)

foo_value = config.get(section,'abc')
print("section {} foo_value = {}".format(section, foo_value))

foo2_value = config.get(section,'abc2')
print("section {} foo2_value = {}".format(section, foo2_value))

print("Done")
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 220 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  ConfigParser(dict_type=) not behaving as expected malonn 5 3,313 Sep-08-2022, 08:42 AM
Last Post: malonn
  Append data to Yaml section tbaror 0 7,004 Feb-09-2022, 06:56 PM
Last Post: tbaror
  using alternate names in configparser Skaperen 6 2,852 Oct-01-2020, 08:27 PM
Last Post: Skaperen
Question configparser.NoSectionError (Python 3.7) alpho 2 5,817 Apr-08-2020, 10:26 PM
Last Post: micseydel
  Create an isolated section in bash? yxk 1 1,959 Feb-27-2020, 11:16 AM
Last Post: Larz60+
  Error in configparser fullstop 12 9,984 Dec-22-2019, 07:36 PM
Last Post: snippsat
  Best section to do a while loop in my code mrapple2020 0 1,653 Apr-15-2019, 01:14 AM
Last Post: mrapple2020
  Help with ConfigParser EricALionsFan 3 3,260 Jul-27-2018, 08:46 PM
Last Post: buran
  Help understanding code section raz631 4 3,256 Dec-14-2017, 09:32 PM
Last Post: raz631

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020