How can I check if Python´s ConfigParser has a section or not?

Posted by Voidcode on Ask Ubuntu See other posts from Ask Ubuntu or by Voidcode
Published on 2012-07-08T20:39:15Z Indexed on 2012/07/08 21:23 UTC
Read the original article Hit count: 569

How can I check if the ConfigParser mainfile has a section or not,

And if it don´t then add a new section to it?

I am trying with this code:

import ConfigParser

config = ConfigParser.ConfigParser()

#if the config file don´t has the section 'globel', then add it to the config-file 
if not config.has_section("globel"):                                
       config.add_section("globel")
       config.set("globel", "settings", "someting")
       config.write(open("/tmp/myfile.conf", "w"))

       print "The new sestion globel is now added!!"
#else just return the the value of globle->settings
else:
       config.read("/tmp/myfile.conf")
       print "else... globle->settings == "+config.get("globel", "settings")

© Ask Ubuntu or respective owner

Related posts about application-development

Related posts about python