reading a config file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s99999999s2003@yahoo.com

    #1

    reading a config file

    hi
    i used ConfigParser to read a config file. I need the config file to
    have identical sections. ie :

    [server]
    blah = "some server"
    [destination]
    blah = "some destination"
    [end]
    end= ''

    [server]
    blah = "some other server"
    [destination]
    blah = "some other destination"
    [end]
    end=''

    and i need to check that every 'server' and 'destination' is followed
    by 'end'

    if i used the 'sections' method, it always show 'server' and
    'destination' and 'end'. how can i iterate all the sections. ie..

    for s in cfg.sections():
    do something...

    or is naming all the sections with different names is a better option?
    thanks

  • Larry Bates

    #2
    Re: reading a config file

    When I need something like this I have employed the following:

    [server_001]
    blah = "some server"
    destination="so me destination"

    [server_002]
    blah = "some other server"
    destination="so me other destination"

    [server_linux1]
    blah = "some other server"
    destination="so me other destination"


    Then I do something like this:

    import ConfigParser
    INI=ConfigParse r.ConfigParser( )
    INI.read(inifil ename)
    serversections=[x for x in INI.sections if x.startswith('s erver_')]
    for serversection in serversections:
    servername=serv ersection.split ('_')[1]
    #
    # Code to operate on the servers here
    #



    Larry Bates


    s99999999s2003@ yahoo.com wrote:[color=blue]
    > hi
    > i used ConfigParser to read a config file. I need the config file to
    > have identical sections. ie :
    >
    > [server]
    > blah = "some server"
    > [destination]
    > blah = "some destination"
    > [end]
    > end= ''
    >
    > [server]
    > blah = "some other server"
    > [destination]
    > blah = "some other destination"
    > [end]
    > end=''
    >
    > and i need to check that every 'server' and 'destination' is followed
    > by 'end'
    >
    > if i used the 'sections' method, it always show 'server' and
    > 'destination' and 'end'. how can i iterate all the sections. ie..
    >
    > for s in cfg.sections():
    > do something...
    >
    > or is naming all the sections with different names is a better option?
    > thanks
    >[/color]

    Comment

    Working...