ConfigParser模块
用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。
import configparser
from configparser import ConfigParser
config =ConfigParser()
config['bitbucket.org'] = {"user":"hg"}
config['YEFAULT'] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9',
'ForwardX11':"yes"}
config['topsecret.server.com'] = {"Host Port":"50022",
"ForwardX11":"no"}
with open('example1.ini', 'w') as configfile:
config.write(configfile)
config.read("example1.ini")
print(config.sections())
print(config.options("bitbucket.org"))
print(config.get("bitbucket.org","user"))
config.remove_option("YEFAULT","compression")#把[YEFAULT][compression]删除
config.add_section("default")#增加section
config.set("default","compression","88")#录入option
config.write(open("example1.ini","w"))#写入文件
print(cf.has_section('default'))#是否有default这个section,结果:True
print(cf.items('default'))#返回default这个section中的子项