博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python读写配置文件
阅读量:7211 次
发布时间:2019-06-29

本文共 2057 字,大约阅读时间需要 6 分钟。

hot3.png

写在前面:封装的一个配置文件操作的类,忘指正!

#coding:utf-8import ConfigParserclass 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

推荐链接:

转载于:https://my.oschina.net/wellsoschina/blog/124223

你可能感兴趣的文章
C# StreamReader.ReadLine统计行数的问题
查看>>
异常测试实践与梳理
查看>>
多者异也
查看>>
tf:'hello tensorflow'
查看>>
RedisConf2018记录--Day 1 sessions
查看>>
CentOS的el5, el6, el7代表什么
查看>>
柏林纪行(中):Node.js Collaboration Summit
查看>>
IT网络通信大变革时代来临 2016中国极客大奖为您找到风向标
查看>>
如何下载WDK
查看>>
硬纪元干货|镁客网萨向东:推动硬科技产业落地,助力传统产业升
查看>>
SSDT&Shadow Hook的实现,完整代码。可编译
查看>>
Spring4-自动装配Beans-通过注解@Autowired在构造方法上
查看>>
MapReduce编程(四) 求均值
查看>>
ASP.NET MVC在IIS6下部署的小技巧
查看>>
asp.net 递归删除文件夹及其子文件夹和所有文件[转]
查看>>
TCP端口状态说明ESTABLISHED、TIME_WAIT、 CLOSE_WAIT
查看>>
Bengio:我留在学术圈为全人类作贡献,而不是为某一个公司赚钱
查看>>
100多个经典常用的PHP功能插件大全实例演示和下载
查看>>
Mac 下iterm2配色方案(高亮)及显示分支
查看>>
使用<meta>来刷新网页效果
查看>>