Parsing files

Skip Montanaro skip at
Tue Feb 10 15:30:06 CET 2004


    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)

        # parse it
        config = ConfigParser.ConfigParser()


        return config


More information about the Python-list mailing list