Python ConfigParser Question

后端 未结 1 1094
温柔的废话
温柔的废话 2021-01-28 03:30

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 fold

1条回答
  •  不思量自难忘°
    2021-01-28 03:54

    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')]
    

    0 讨论(0)
提交回复
热议问题