python读写ini配置文件方法实例分析
更新时间:2015年06月30日 16:58:30 作者:不吃皮蛋
这篇文章主要介绍了python读写ini配置文件方法,实例分析了Python针对ini配置文件的相关读写技巧,需要的朋友可以参考下
本文实例讲述了python读写ini配置文件方法。分享给大家供大家参考。具体实现方法如下:
import ConfigParser import os class ReadWriteConfFile: currentDir=os.path.dirname(__file__) filepath=currentDir+os.path.sep+"inetMsgConfigure.ini" @staticmethod def getConfigParser(): cf=ConfigParser.ConfigParser() cf.read(ReadWriteConfFile.filepath) return cf @staticmethod def writeConfigParser(cf): f=open(ReadWriteConfFile.filepath,"w"); cf.write(f) f.close(); @staticmethod def getSectionValue(section,key): cf=ReadWriteConfFile.getConfigParser() return cf.get(section, key) @staticmethod def addSection(section): cf=ReadWriteConfFile.getConfigParser() allSections=cf.sections() if section in allSections: return else: cf.add_section(section) ReadWriteConfFile.writeConfigParser(cf) @staticmethod def setSectionValue(section,key,value): cf=ReadWriteConfFile.getConfigParser() cf.set(section, key, value) ReadWriteConfFile.writeConfigParser(cf) if __name__ == '__main__': ReadWriteConfFile.addSection( 'messages') ReadWriteConfFile.setSectionValue( 'messages','name','sophia') x=ReadWriteConfFile.getSectionValue( 'messages','1000') print x
希望本文所述对大家的Python程序设计有所帮助。
相关文章
python中将\\uxxxx转换为Unicode字符串的方法
这篇文章主要介绍了python中将\\uxxxx转换为Unicode字符串的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-09-09python GUI库图形界面开发之PyQt5中QWebEngineView内嵌网页与Python的数据交互传参详细方法
这篇文章主要介绍了python GUI库图形界面开发之PyQt中QWebEngineView内嵌网页与Python的数据交互详细方法实例,需要的朋友可以参考下2020-02-02Python还能这么玩之只用30行代码从excel提取个人值班表
公司实行项目值班制度,拿到值班表,看到全部的值班信息,要去查找自己的值班信息,是一件头痛的事情.作为程序员,当然要简化,将自己的信息提炼出来,需要的朋友可以参考下2021-06-06
最新评论