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程序设计有所帮助。 微信分享声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系[email protected]核实处理。 相关标签:python 读写 ini配置文件推荐:PHP从基础到实战教程视频 上一篇:python实现的希尔排序算法实例下一篇:python获取一组汉字拼音首字母的方法 |
CopyRight 2018-2019 实验室设备网 版权所有 |