3

I am trying to fetch data from a .cfg (configuration file) in python but I am always getting the error

ConfigParser.MissingSectionHeaderError: File contains no section headers.

The screenshot of the problem

Here is the link for the .cfg file:

http://www.mediafire.com/file/66k9m7trxo7krx9/a.cfg/file

Alex Sergeenko
  • 642
  • 5
  • 22
joginder singh
  • 41
  • 1
  • 1
  • 7

3 Answers3

4

Most probably you have parameters without section, e.g.:

name1 = value1
name2 = value2

According documentation there should be a section.

I may suggest the following solutions:

Add section explicitly

Add section in ini file, e.g:

[default]
name1 = value1
name2 = value2

Add section implicitly

from configparser import ConfigParser

parser = ConfigParser()
with open('config.ini') as stream:
    parser.read_string('[default]\n' + stream.read())

# now you can use it for parameters without section
print(parser.get('default', 'name1'))
Maxim Suslov
  • 4,335
  • 1
  • 35
  • 29
2

The ConfigParser module assumes that config files have section headers. Section headers are used to keep configurations of one type together. Write [Default] in the first line of your configuration file. This will tell ConfigParser that there is only one header and that is the default.

0

You are adding JSON file instead of config file in format:

[OPTIONS]
option1 = foo
option2 = bar