python configParser 模块
admin
2023-07-24 21:03:02
0

configParser 模块用于操作配置文件
配置文件(INI文件)由节(section)、键、值组成
1、config=ConfigParser.ConfigParser() 创建ConfigParser实例
2、config.sections() 返回配置文件中节序列
3、config.options(section) 返回某个项目中的所有键的序列
4、config.get(section,option) 返回section节中,option的键值
5、config.add_section(str) 添加一个配置文件节点(str)
6、config.set(section,option,val) 设置section节点中,键名为option的值(val)
7、config.read(filename) 读取配置文件
8、config.write(obj_file) 写入配置文件
9、config.get() 返回文本 config.getint()返回整数
10、config.remove_option( section, option)
11、config.remove_section( section)
案例:
import ConfigParser
import os
class ReadWriteConfFile:
currentDir=os.path.dirname(file) filepath=currentDir+os.path.sep+"inetMsgConfigure.ini"
@staticmethod
br/>filepath=currentDir+os.path.sep+"inetMsgConfigure.ini"
@staticmethod
cf=ConfigParser.ConfigParser()
cf.read(ReadWriteConfFile.filepath)
return cf@staticmethod
br/>@staticmethod
f=open(ReadWriteConfFile.filepath,"w"); cf.write(f)
f.close();
@staticmethod
br/>cf.write(f)
f.close();
@staticmethod
cf=ReadWriteConfFile.getConfigParser()
return cf.get(section, key)@staticmethod
br/>@staticmethod
cf=ReadWriteConfFile.getConfigParser()
allSections=cf.sections()
if section in allSections:return
else:
cf.add_section(section)
ReadWriteConfFile.writeConfigParser(cf)
@staticmethod
br/>return
else:
cf.add_section(section)
ReadWriteConfFile.writeConfigParser(cf)
@staticmethod
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')

相关内容

热门资讯

中国“避暑神器”在欧洲卖爆了 2026.06.27作者 | 第一财经 王珍中国企业推出易安装的移动分体式空调,最近被欧洲人视作“避...
苹果Mac、iPad全线涨价!... 快科技6月26日消息,据媒体报道,昨晚,苹果突然官宣上调全球多个市场的Mac、iPad等多款硬件产品...
这个“杀手”太冷了 每一次重大科技突破,都会催生作战方式的深刻变革,从而形成具有鲜明时代特征的非对称战争。近年来,科技革...
原创 [... [微风]一位性学家最近说了一句挺让人后背发凉的话:AI女性机器人,只要做到能解决男性的生理需求这一步...
原创 戳... 编辑:[碰拳] “好位置快被占完了”,这话不是哪个自媒体在贩卖焦虑,是中国星链的总指挥在公开场合说的...
市场监管总局:加快智能体、具身... IT之家 6 月 27 日消息,据央视新闻 6 月 25 日报道,市场监管总局正会同相关部门,加快智...
5G-A用户破亿,华为轮值董事... 6月26日,2026世界移动通信大会(MWC26上海)在上海新国际博览中心举行。在大会赛场,一场机器...
大爆炸后约18亿年:6个星系正... IT之家 6 月 26 日消息,科技媒体 Space 今天(6 月 26 日)发布博文,报道称天文学...
官方通报“果园里密密麻麻立满监... 6月27日,巴东县东瀼口镇人民政府发布关于雷家坪社区一果园安装监控设备有关情况的通报:2026年6月...
女子被急流冲走,卡在石缝中身亡 6月25日下午,广东清远英德市石牯塘镇落龙潭野外溪谷发生一起意外,一名女子戏水时被湍急水流冲走,困在...