reading a config file

S

s99999999s2003

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
 
L

Larry Bates

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

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

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

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


Then I do something like this:

import ConfigParser
INI=ConfigParser.ConfigParser()
INI.read(inifilename)
serversections=[x for x in INI.sections if x.startswith('server_')]
for serversection in serversections:
servername=serversection.split('_')[1]
#
# Code to operate on the servers here
#



Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top