• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python读写ini配置文件

python 水墨上仙 2001次浏览

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读写ini配置文件
喜欢 (0)
加载中……