Will Dowling
Will Dowling

Reputation: 33

Python error : ConfigParser.NoSectionError: No section:

I'm new to Python and would like to read a config file using the ConfigParser module. Here is my config file:

[Server]
server_name= localhost

And here is my code:

#!/usr/bin/env python

import ConfigParser

config = ConfigParser.ConfigParser()
config.read=('config.cfg')

print config.get('Server', 'server_name')

I try to run this and it gives me the error:

    File "./read_config.py", line 10, in <module>
    print config.get('Server', 'server_name')
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ConfigParser.py", line 531, in get
    raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'Server'

It can't find the server section. The config.cfg file is in the same directory as the Python script and I have even gave the full path but still comes up with the same error. I have tried Python 2.6 and 2.7 on both OSX and Debian 6. What am I missing here?

Upvotes: 3

Views: 26592

Answers (1)

NPE
NPE

Reputation: 500357

You have an extra character on the following line:

config.read('config.cfg')

I've removed the =.

With the =, the code is valid syntactically but does something different to what's intended.

Upvotes: 7

Related Questions