用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser
。
下面主要说一下Python基于ConfigParser模块针对配置文件的创建、读取、写入、判断等相关操作技巧;
创建配置文件
import configparser
config = configparser.ConfigParser() # 生成一个处理对象;
#生成一个默认配置;
config["DEFAULT"] = {'Server': 'localhost',
'Compression': 'yes',
'CompressionLevel': '9'}
#生成其他配置组;
config['bitbucket.org'] = {'User': 'robin'}
config['topsecret.server.com'] = {'Host Port' : "5022",
'ForwardX11' : 'no'}
#写入配置文件;
with open('example.ini','w') as configfile:
config.write(configfile)
执行结果: