Search code examples
arcgispython

Python ConfigParser Question


Does the Config file for the ConfigParser have to be named "Config.ini" in order to work?

I want the name to be "1Config.ini" so that it appears at the top of a folder dir.

This is what I have currently

config = ConfigParser.ConfigParser()
config.read(Revision[0:Revision.rfind('\\')] + "\1Config.ini")

Type = config.get("myvars", "Type")

I get this error however when the file and code is named "1Config.ini"

<class 'ConfigParser.NoSectionError'>: No section: 'myvars'

Solution

  • What's the output of the following? Make sure it's a valid file name.

    >>> print Revision[0:Revision.rfind('\\')] + "\1Config.ini"
    

    Ideally use os.path.join instead of concatenating strings:

    import os
    filename = os.path.join(Revision[0:Revision.rfind('\\')], "Config.ini")
    config.read(filename)
    

    You probably shouldn't name your variable Type, because type is a built-in function/module and it'd be confusing.

    Type = config.get("myvars", "Type")
    

    And no, config files can be named anything:

    >>> a = ConfigParser.ConfigParser()
    >>> a.read("E:/Documents/2012/config.test") # where config.test is the example from the documentation
    ['E:/Documents/2012/config.test']
    >>> a.sections()
    ['My Section']
    >>> a.items(a.sections()[0])
    [('foodir', 'frob/whatever'),
     ('dir', 'frob'),
     ('long', 'this value continues\nin the next line')]