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

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

Pandas数据离散化原理及实例解析

Pandas数据离散化原理及实例解析

这篇文章主要介绍了Pandas数据离散化原理及实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 为什么要离散化 连续属性离...

python连接mysql数据库示例(做增删改操作)

一、相关代码数据库配置类 MysqlDBConn.py 复制代码 代码如下:#encoding=utf-8'''Created on 2012-11-12Mysql Conn连接类'''...

python处理document文档保留原样式

document文档格式、线段、图片、页眉页脚等都不变,供大家参考,具体内容如下 # -*- coding: utf-8 -*- # @Time : 2019/5/6 11:46...

Java分治归并排序算法实例详解

Java分治归并排序算法实例详解

本文实例讲述了Java分治归并排序算法。分享给大家供大家参考,具体如下: 1、分治法 许多有用的算法在结构上是递归的:为了解决一个给定的问题,算法一次或多次递归地调用其自身以解决紧密相关...

python实现批量视频分帧、保存视频帧

本篇博客介绍利用python脚本实现视频分帧,并将每一帧保存到本地。主要基于opencv包来实现,在运行代码前确保opencv包已正确安装。下面是主要代码: import os i...