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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With