python 写配置文件_python读写配置文件

#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")

#-----------------conf.ini--------------------

#[db]

#db_host = 127.0.0.1

#db_port = 3306

#db_user = root

#db_pass = wells

#

#[concurrent]

#thread = 10

#processor = 20

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值