configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意。
配置文件(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)
写入配置文件
综合实例(更新配置文件)
#coding=utf-8
import configparser
def writeConfig(filename):
config = configparser.ConfigParser()
# set db
section_name = 'db'
config.add_section( section_name )
config.set( section_name, 'dbname', 'MySQL')
config.set( section_name, 'host', '127.0.0.1')
config.set( section_name, 'port', '80')
config.set( section_name, 'password', '123456')
config.set( section_name, 'databasename', 'test')
# set app
section_name = 'app'
config.add_section( section_name )
config.set( section_name, 'loggerapp', '192.168.20.2')
config.set( section_name, 'reportapp', '192.168.20.3')
# write to file
config.write( open(filename, 'a') )
def updateConfig(filename, section, **keyv):#**和*自定义都是一样的,**后面也是自定义,后面不是数字就行
config = configparser.ConfigParser()
config.read(filename)
print (config.sections())
for section in config.sections():
print ("[",section,"]")
items = config.items(section)
for item in items:
print ("\t",item[0]," = ",item[1])
print (config.has_option("dbname", "MySQL"))
print (config.set("db", "dbname", "11"))
print ("...............")
for key in keyv:
print ("\t",key," = ", keyv[key])
config.write( open(filename, 'r+') )
if __name__ == '__main__':
file_name = 'test.ini'
writeConfig(file_name)
updateConfig(file_name, 'app', reportapp = '192.168.100.100')
print ("end__")