一、简述
在很多情况下,我们都需要修改配置文件,但是,有些配置文件,如mysql数据库的配置文件怎么修改呢?我们今天就来写一下,用于生产和修改常见配置文件的模块:configparser。
二、配置文件格式
1、配置文件格式
[DEFALUT]compressionlevel = 9serveraliveinterval = 45compression = yesforwardx11 = yes[bitbucket.org]user = hg[topsecret.server.com]host port = 50022forwardx11 = no
三、创建配置文件
1、创建配置文件
说明:其实有的时候我们很少创建,除非是用系统管理,一般直接修改就可以了,但是还是要掌握的。
代码如下:
import configparser #导入configparser模块#创建一个对象config = configparser.ConfigParser()#配置默认全局配置组config["DEFALUT"] = {"ServerAliveInterval":"45", "Compression":"yes", "CompressionLevel":"9" }#配置第一个其他组config["bitbucket.org"] = {}#没有没有赋给一个变量,直接赋值config["bitbucket.org"]["User"] = 'hg'#配置第二个其他组config["topsecret.server.com"] = {}#这边就赋给一个变量topsecret = config["topsecret.server.com"]#通过变量赋值topsecret["Host Port"] = '50022'topsecret["ForwardX11"] = 'no'#给全局配置组赋值config["DEFALUT"]["ForwardX11"] = "yes"#操作完毕,把配置的内容写入一个配置文件中with open("example.ini","w") as configfile: config.write(configfile)
四、读取配置文件
1、读取配置组
>>> import configparser>>> config = configparser.ConfigParser()>>> config.sections() #不读取配置文件,组名列表为空[]>>> config.read("example.ini") #读取配置文件,返回配置文件名['example.ini']>>> config.sections() #返回除默认配置组的其他组名['bitbucket.org', 'topsecret.server.com']>>> config.defaults() #读取默认配置组,并返回有序字典OrderedDict([('compressionlevel', '9'), ('serveraliveinterval', '45'), ('compression', 'yes'), ('forwardx11', 'yes')])
2、组名是否存在
>>> 'bitbucket.org' in config #组名存在True>>> 'zhangqigao.org' in config #组名不存在False
3、读取组内的值
>>> config["bitbucket.org"]["User"] #读取"bitbucket.org"配置组中的值'hg'>>> config["DEFAULT"]["Compression"] #读取默认配置组中的值'yes'>>> topsecret = config['topsecret.server.com'] #把配置组赋给一个对象>>> topsecret['ForwardX11'] #通过对象获取值'no
4、 循环获取组内的key值
>>> for key in config["bitbucket.org"]: #循环打印bitbucket.org组下的key值... print(key)...#输出,只打印默认组和bitbucket.org组的key值usercompressionlevelserveraliveintervalcompressionforwardx11>>> for key in config["topsecret.server.com"]:#循环打印topsecret.server.com组下的key值... print(key)...#输出,只打印默认组和topsecret.server.com组的key值host portforwardx11compressionlevelserveraliveintervalcompression
注:默认组是全局的,所以循环遍历key值时,会遍历从默认组和需要遍历的组一起遍历出来。
五、configparser增删改查语法
1、配置文件名i.cfg
[DEFAULT]k1 = v1k2 = v2[section1]k3 = v3k4:v4[section2]k5 = 5
2、读i.cfg
import configparserconfig = configparser.ConfigParser()config.read("i.cfg")sec = config.sections()print(sec)#输出['section1', 'section2']options = config.options("section2") #返回默认组和section2组的key值print(options)#输出['k5', 'k1', 'k2']item_list = config.items("section2") #返回默认组和section2组的key-value值print(item_list)#输出[('k1', 'v1'), ('k2', 'v2'), ('k5', '5')]val1 = config.get("section2","k1") #获取section2组中k1对应的值,是否可取是按照上面返回的列表print(val1)#输出v1val2 = config.getint("section2","k5") #返回section2中k5的值,这个值返回的int类型的print(val2)#输出5
3、改写i.cfg
①删除section和option
import configparserconfig = configparser.ConfigParser()config.read("i.cfg")config.remove_option("section1","k3") #删除section1组下的k3config.remove_section("section2") #删除section2组with open("i.cfg2","w") as f: #重新写入一个文件 config.write(f)#输出,写入文件的内容[DEFAULT]k1 = v1k2 = v2[section1]k4 = v4
②添加section
import configparserconfig = configparser.ConfigParser()config.read("i.cfg")sec = config.has_option("section2","k5") #是否存在section2组内有k5print(sec)#输出Truesec = config.has_section("zhangqigao") #是否存在zhangqigao组print(sec)#输出Falseconfig.add_section("zhangqigao") #添加section组zhangqigaoconfig.add_section("zhangqigao") #重新写入到一个配置文件中with open("i.cfg3","w") as f: config.write(f)
③添加或者设置option
import configparserconfig = configparser.ConfigParser()config.read("i.cfg")config.set("zhangqigao","z","18") #设置或者添加zhangqigao中option值with open("i.cfg3","w") as f: #重新写入文件中 config.write(f)