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

作者:不吃皮蛋 时间:2022-10-09 22:02:28 

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

标签:python,ini
0
投稿

猜你喜欢

  • 利用Python实现简单的Excel统计函数

    2021-09-27 09:21:09
  • 在ASP.NET页面中如何利用JAVASCRIPT脚本向IFRAMES和POPUP传值

    2007-09-23 13:18:00
  • Python PSO算法处理TSP问题详解

    2022-12-02 02:39:24
  • Python实现希尔排序算法的原理与用法实例分析

    2021-10-19 17:44:08
  • mysql本地登录无法使用端口号登录的解决方法

    2024-01-25 06:51:30
  • python实现银行管理系统

    2021-07-09 00:33:49
  • python用turtle库绘画圣诞树

    2023-05-24 19:35:40
  • MySQL的数据库常用命令 超级实用版分享

    2012-01-05 18:58:00
  • 分享一个Emeditor压缩样式的宏

    2010-08-16 12:30:00
  • Python实现冒泡,插入,选择排序简单实例

    2022-01-07 06:47:25
  • Python函数实现学员管理系统

    2023-06-22 03:32:30
  • 深入学习Python中的上下文管理器与else块

    2023-05-11 08:57:53
  • BootstrapTable+KnockoutJS相结合实现增删改查解决方案(三)两个Viewmodel搞定增删改查

    2024-04-28 09:36:56
  • 如何防止Application对象在多线程访问中出现错误?

    2009-11-22 19:18:00
  • Django上线部署之IIS的配置方法

    2023-02-18 05:17:52
  • Pygame Surface创建图像的实现

    2023-07-10 13:44:26
  • Django实现自定义路由转换器

    2021-09-10 05:49:04
  • mysql中的临时表如何使用

    2024-01-28 10:19:21
  • Python全局变量操作详解

    2022-04-18 04:22:00
  • ASP使用MYSQL数据库全攻略

    2009-11-08 18:27:00
  • asp之家 网络编程 m.aspxhome.com