标签:文件 def os.path write val gets etc 序列 config
configParser 模块用于操作配置文件案例:
import ConfigParser
import os
class ReadWriteConfFile:
currentDir=os.path.dirname(file) @staticmethod
br/>filepath=currentDir+os.path.sep+"inetMsgConfigure.ini"
@staticmethod
def getConfigParser():
cf=ConfigParser.ConfigParser()
cf.read(ReadWriteConfFile.filepath)
return cfbr/>@staticmethod
def writeConfigParser(cf):
f=open(ReadWriteConfFile.filepath,"w"); br/>cf.write(f)
f.close();
@staticmethod
def getSectionValue(section,key):
cf=ReadWriteConfFile.getConfigParser()
return cf.get(section, key)br/>@staticmethod
def addSection(section):
cf=ReadWriteConfFile.getConfigParser()
allSections=cf.sections()
if section in allSections:br/>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‘)
标签:文件 def os.path write val gets etc 序列 config
原文地址:http://blog.51cto.com/weadyweady/2133117