add
1、新增一个section
# 添加一个select conf.add_section("emali_tel") print(conf.sections())
2、section里面新增key和value
# 往select添加key和value conf.set("emali_tel", "sender", "yoyo1@tel.com") conf.set("emali_tel", "port", "265")
3、添加section 和 option
import configparser config = configparser.ConfigParser() config.read("ini", encoding="utf-8") if not config.has_section("default"): # 检查是否存在section config.add_section("default") if not config.has_option("default", "db_host"): # 检查是否存在该option config.set("default", "db_host", "1.1.1.1") config.write(open("ini", "w"))
1
2
3
4
5
6
7
8
9
10
11
12
|
[db] db_host = 127.0.0.1 db_port = 69 db_user = root db_pass = root [concurrent] thread = 10 processor = 20 [default] db_host = 1.1.1.1 |
write写入
1、write写入有两种方式,一种是删除原文件内容,重新写入:w
conf.write(open(cfgpath, "w")) # 删除原文件重新写入
另外一种是在原文件基础上继续写入内容,追加模式写入:a
conf.write(open(cfgpath, "a")) # 追加模式写入
2、前面讲的remove和set方法并没有真正的修改ini文件内容,只有当执行conf.write()方法的时候,才会修改ini文件内容,举个例子:在ini文件上追加写入一项section内容
# coding:utf-8 import configparser import os curpath = os.path.dirname(os.path.realpath(__file__)) cfgpath = os.path.join(curpath, "cfg.ini") print(cfgpath) # cfg.ini的路径 # 创建管理对象 conf = configparser.ConfigParser() # 添加一个select conf.add_section("emali_tel") print(conf.sections()) # 往select添加key和value conf.set("emali_tel", "sender", "yoyo1@tel.com") conf.set("emali_tel", "port", "265") items = conf.items('emali_tel') print(items) # list里面对象是元祖 conf.write(open(cfgpath, "a")) # 追加模式写入
运行后会发现ini文件最后新增了写入的内容了
3、写入文件
以下的几行代码只是将文件内容读取到内存中,进过一系列操作之后必须写回文件,才能生效
import configparser config = configparser.ConfigParser() config.read("ini", encoding="utf-8")
写回文件的方式如下:(使用configparser的write方法)
config.write(open("ini", "w")) # 删除原文件重新写入
综合实例:
#coding=utf-8 import ConfigParser def writeConfig(filename): config = ConfigParser.ConfigParser() #创建ConfigParser实例 # set db section_name = 'db' config.add_section( section_name ) #添加一个配置文件节点(str 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") #设置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__"