Forums

ConfigParser Problem

I'm having some trouble with ConfigParser. It once worked and now seems not to.

This is what the section of my code looks like that parses the config file:

from ConfigParser import SafeConfigParser

parser = SafeConfigParser()
parser.read('config.txt')
T1 = parser.get('config', 'THING1')
T2 = parser.get('config', 'THING2')

This is what the config file looks like:

[config]

THING1 = some_setting

THING2 = some_setting2

But I get this error when running the application:

2013-07-12 16:37:09,360 :Traceback (most recent call last):

2013-07-12 16:37:09,362 :ConfigParser.NoSectionError: No section: 'config'

Clearly the section "config" does exist. This same code works fine on my desktop and has worked in the past on PA. Any ideas?

Hm, that code snippet with that config file works fine for me at a bash prompt on PA. Perhaps there's something like a permissions problem reading the file and ConfigParser is catching the error and instead returning something less helpful?

EDIT

Indeed, I just did chmod 000 config.txt and tried again and got the same error you're seeing. My guess is that your file is either missing or you're attempting to load it from the wrong directory, or there's some other permissions error. Try loading it with an absolute path name and check its permissions.

If you want, I could take a look at your code -- just send us a message using the "Send feedback" link at the top saying where the code and the config file are...

As an aside, you can see from the source of the read() method that it catches and ignores any IOError in any config file. I seem to remember bumping into this behaviour myself awhile ago and being intensely annoyed by it. I can see some justification because it caters for cases where configuration files are optional, but at the very least there should be a parameter to change the behaviour. Essentially you have to use something like os.access() yourself before passing a file in if you want to catch errors.