• 技术文章 >后端开发 >Python教程

    python读写ini配置文件方法实例分析

    2016-06-10 15:10:05原创337
    本文实例讲述了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程序设计有所帮助。

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    上一篇:python实现的希尔排序算法实例 下一篇:python获取一组汉字拼音首字母的方法
    大前端线上培训班

    相关文章推荐

    • Django框架中方法的访问和查找• Python文件和目录操作详解• Python中使用异常处理来判断运行的操作系统平台方法• Python Queue模块详解• Python统计列表中的重复项出现的次数的方法

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网