码迷,mamicode.com
首页 > 编程语言 > 详细

python ConfigParser例子02

时间:2014-07-29 11:25:16      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:io   cti   ar   python   ad   ef   return   date   

#coding:utf-8

 

import ConfigParser

 

class Conf():

    

    def __init__(self,name):

        self.name = name

        self.cp = ConfigParser.ConfigParser()

        self.cp.read(name)

         

            

    def getSections(self):

        return self.cp.sections()

    

    def getOptions(self, section):

        if self.cp.has_section(section):

            return self.cp.options(section)

    

    def getItems(self, section):

        if self.cp.has_section(section):

            return self.cp.items(section)

        

    def getValue(self, section, option):

        if self.cp.has_option(section, option):

            return self.cp.get(section, option)

    

    def setSection(self, section):

        if not self.cp.has_section(section):

            self.cp.add_section(section)

            self.cp.write(open(self.name,‘w‘))

    

    def setValue(self, section, option, value):

        if not self.cp.has_option(section, option):

            self.cp.set(section, option, value)

            self.cp.write(open(self.name,‘w‘))

    

    def delSection(self, section):

        if self.cp.has_section(section):

            self.cp.remove_section(section)

            self.cp.write(open(self.name,‘w‘))

    

    def delOption(self, section, option):

        if self.cp.has_option(section, option):

            self.cp.remove_option(section, option)

            self.cp.write(open(self.name,‘w‘))

            

    def updateValue(self, section, option, value):

        if self.cp.has_option(section, option):

            self.cp.set(section, option, value)

            self.cp.write(open(self.name,‘w‘))

 

if __name__ == "__main__":

    conf = Conf("confx.ini")

    conf.setSection("add")

    conf.setValue("add", "version", "v1.0")

    conf.updateValue("add", "version", "v1.1")

    

    print conf.getItems("add")

    print conf.getSections()

    conf.delSection("add")

    

 

python ConfigParser例子02,布布扣,bubuko.com

python ConfigParser例子02

标签:io   cti   ar   python   ad   ef   return   date   

原文地址:http://www.cnblogs.com/mhxy13867806343/p/3874025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!