3

I'm using configparser in order to read and modify automatically a file conf named 'streamer.conf'. I'm doing this :

import configparser

config = configparser.ConfigParser()
config.read('C:/Users/../Desktop/streamer.conf')

And then it breaks apart with this Error message :

MissingSectionHeaderError: File contains no section headers.
file: 'C:/Users/../Desktop/streamer.conf', line: 1
u'input{\n'

What might be wrong? Any help appreciated.

5 Answers 5

6

just specify right encoding

config.read(config_file_path, encoding='utf-8-sig')
  • "utf-8-sig" for UTF-8 with BOM
  • "utf-8" for UTF-8 without BOM
4

As mentioned in other answers the config file needs to be INI format, but the actual error you're seeing is due to the fact that the requested config file is missing any section headers (as mentioned in the error message) - these are names enclosed in square brackets which provide a header for a section of the INI file. The default header is [DEFAULT] e.g.

[DEFAULT]
 config_item1='something'
 config_item2=2
1

I got the same error message when I created a pip.conf file. In my case, I had inadvertently created a UTF-8 file with BOM (byte-order marker) instead of a plain UTF-8 file (with no BOM).

So, check to make sure you have a plain text file. If you're not sure, you can open the file in a hex editor and check the first byte(s).

1

ConfigParser parse UTF-8 file with BOM(xef xbb xbf)

    u = open("setting.ini").read().decode("utf-8-sig").encode("utf-8")
    fp = tempfile.TemporaryFile()
    fp.write(u)
    fp.seek(0)

    conf = ConfigParser.ConfigParser()
    conf.readfp(fp)
1
  • This answer is useful, maybe add an explanation of what the BOM is and how the first line removes it? Jan 27, 2017 at 21:07
0

You didn't include streamer.conf, but from the error message, it's not in the right format. configparser is used for parsing "INI" files:

[section1]
setting1 = value
setting2 = value

[section2]
setting3 = value
setting1 = value

etc.

2
  • J Earls : I got it ! But is there a way in which I can deal with .conf files ? Oct 3, 2016 at 14:05
  • ".conf files" has no specific meaning. Given the one tiny snippet from the error message, it's not XML, JSON, INI, CSV. You will most likely have to write your own file parser for this.
    – J Earls
    Oct 4, 2016 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.