Parsing files
Skip Montanaro
skip at pobox.com
Tue Feb 10 09:30:06 EST 2004
>> http://cvs.sourceforge.net/viewcvs.py/spambayes/spambayes/spambayes/Options.py?rev=1.104
Rob> thanks, I've taken a look but I'm still unclear as to how the
Rob> defaults work. At the moment my config file has no section but
Rob> when I create a new ConfigParser it complains that no sections are
Rob> defined (so the parse fails). Can I fool the ConfigParser by
Rob> defining an 'empty' default and if do you know how?
I believe ConfigParser (and INI-type files in general) expect all options to
be grouped into sections. If you don't need/want sections, why not just add
a [general] section into which you put all your options? Failing that, you
could probably fudge things with something like this (untested):
import tempfile
import ConfigParser
def parse_config_file(f):
fd, fn = tempfile.mkstemp()
# create a temporary config file with a dummy section
cfg = os.fdopen(fd)
cfg.write("[general]\n")
cfg.write(file(f).read())
cfg.close()
# parse it
config = ConfigParser.ConfigParser()
config.read(fn)
os.unlink(fn)
return config
Skip
More information about the Python-list
mailing list