views:

276

answers:

3

I have the following:

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

How can I close the file opened with config.read?

In my case, as new sections/data are added to the config.cfg file, i update my wxtree widget, however, it only updates once, and i suspect it's because config.read leaves the file open.

and while at it, what is the main difference between ConfigParser and RawConfigParser?

+2  A: 

To test your suspicion, use ConfigParser.readfp() and handle opening and closing of the file by yourself. Make the readfp call after the changes are made.

config = ConfigParser()
#...on each change
fp = open('connections.cfg')
config.read(fp)
fp.close()
sections = config.sections()
gimel
+2  A: 

Use readfp instead of read:

config = ConfigParser()
fp = open('connections.cfg')
config.readfp(fp)
sections = config.sections()

fp.close()
NicDumZ
+2  A: 

The difference between ConfigParser and RawConfigParser is that ConfigParser will attempt to "magically" expand references to other config variables, like so:

x = 9000 %(y)s
y = spoons

In this case, x will be 9000 spoons, and y will just be spoons. If you need this expansion feature, the docs recommend that you instead use SafeConfigParser. I don't know what exatly the difference between the two is. If you don't need the expansion (you probably don't) just need RawConfigParser.

Paul Fisher
thanks for this
lyrae