Python入门之ConfigParser模块
ConfigParser,它实现了一种基本的配置语言,它提供的结构类似于Microsoft Windows INI文件中所发现的结构。您可以使用它编写程序配置文件,这些配置文件可以很容易地由终端用户定制。块所解析的ini配置文件是由多个section构成,每个section名用中括号‘[]’包含,每个section下可有多个配置项类似于key-value形式,例如:
[DEFAULT] #默认配置,各节点继承 ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no
1、ConfigParser模块其操作基本分为三类:1)初始化;2)读取配置;3)写入配置。
1.1 ConfigParser 初始化
使用ConfigParser 首选需要初始化实例,并读取配置文件:
cf = ConfigParser.ConfigParser() cf.read("配置文件名")
1.2 基本的读取配置文件
-read(filename) 直接读取ini文件内容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对
-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
1.3 基本的写入配置文件
-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
-write(strout) 将对configparser类的修改写入
2、ConfigParser模块创建新配置文件
#!/usr/bin/env python3 import configparser #初始化配置实例config config = configparser.ConfigParser() #创建默认配置;默认配置会被各节点继承 config["DEFAULT"] = {"ServerAliveInterval" : 45, "Compression" : "yes", "CompressionLevel" : 9, "ForwardX11" : "yes"} #创建节点 config["bitbucket.org"] = {} #或bitbucket = config["bitbucket.org"] config["topsecret.server.com"] = {} #或topsecret = config["topsecret.server.com"] #添加节点内容 config["bitbucket.org"]["user"] = "hg" #或bitbucket["user"] = "hg" config["topsecret.server.com"]["Port"] = "50022" #或topsecret["Port"] = "50022" config["topsecret.server.com"]["ForwardX11"] = "no" #或topsecret['ForwardX11'] = 'no' #将config写入配置文件example.ini with open("/mnt/example.ini","w") as cf: config.write(cf) #或config.write(open("/mnt/example.ini","w")) 执行结果: [root@localhost 20170720]# ./py_config.py #执行脚本 [root@localhost 20170720]# cat /mnt/example.ini #脚本执行成功后;查看/mnt/example.ini [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [topsecret.server.com] port = 50022 forwardx11 = no
3、ConfigParser模块读取配置文件内容
#!/usr/bin/env python3 import configparser config = configparser.ConfigParser() #初始化配置实例config config.read("/mnt/example.ini") #读取已存在的配置文件 #读取所有节点名称 secs = config.sections() print("All sections: ",secs) #读取指定节点内的所有options options = config.options('topsecret.server.com') print("options of topsecret.server.com: ",options) #读取指定节点内的所有items,即section的options和值 items = config.items('topsecret.server.com') print("items of topsecret.server.com: ",items) #读取指定节点内指定option的值 option_port = config.get('topsecret.server.com','port') #读取节点内指定option和值,以str类型返回 option_forward = config.get('topsecret.server.com','forwardx11') #读取节点内指定option和值,以str类型返回 option_alive = config.getint('topsecret.server.com','serverAliveInterval') #读取节点内指定option和值,以int类型返回;如果option在节点内没有,则会继承DEFAULT节点option内容 print("Port of topsecret.server.com: ",option_port) print("Forwardx11 of topsecret.server.com: ",option_forward) print("ServerAliveInterval of topsecret.server.com: ",option_alive) 执行结果: [root@localhost 20170720]# ./py_content.py All sections: ['bitbucket.org', 'topsecret.server.com'] options of topsecret.server.com: ['port', 'forwardx11', 'serveraliveinterval', 'compression', 'compressionlevel'] items of topsecret.server.com: [('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'no'), ('port', '50022')] Port of topsecret.server.com: 50022 Forwardx11 of topsecret.server.com: no ServerAliveInterval of topsecret.server.com: 45
注:子节点没有的内容,会自动继承DEFAULT节点内的默认配置;子节点存在的内容则会覆盖DEFAULT节点内的默认配置。
4、ConfigParser模块修改配置文件内容
原配置文件
[root@localhost mnt]# cat example.ini [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [topsecret.server.com] port = 50022 forwardx11 = no
修改配置文件脚本
#!/usr/bin/env python3 import configparser config = configparser.ConfigParser() #初始化配置实例config config.read("/mnt/example.ini") #读取已存在的配置文件 #删除指定节点;并保存至新配置文件example1.ini内 config.remove_section('topsecret.server.com') config.write(open('/mnt/example1.ini', "w")) #判断是否存在节点topsecret.server.com;存在返回True,反之False top_flag = config.has_section('topsecret.server.com') print(top_flag) #添加节点test.server.com;添加节点内容;并保存至新配置文件example2.ini内 config.add_section("test.server.com") config["test.server.com"]["User"] = "chengd" config.write(open('/mnt/example2.ini', "w")) #修改配置文件指定option的值;并保存至新配置文件example3.ini内 config.set('test.server.com','Port',"8080") config.write(open('/mnt/example3.ini', "w")) #删除指定节点指定option的值;并保存至新配置文件example4.ini内 config.remove_option('test.server.com','Port') config.write(open('/mnt/example4.ini', "w"))
执行脚本后的各配置文件:
[root@localhost mnt]# ls example1.ini example2.ini example3.ini example4.ini example.ini [root@localhost mnt]# cat example1.ini [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [root@localhost mnt]# cat example2.ini [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [test.server.com] user = chengd [root@localhost mnt]# cat example3.ini [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [test.server.com] user = chengd port = 8080 [root@localhost mnt]# cat example4.ini [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [test.server.com] user = chengd [root@localhost mnt]# cat example.ini [DEFAULT] serveraliveinterval = 45 compression = yes compressionlevel = 9 forwardx11 = yes [bitbucket.org] user = hg [topsecret.server.com] port = 50022 forwardx11 = no
注意:ConfigParser模块中配置文件不区分大小写。
***********************************************************
学习永远不晚。——高尔基
***********************************************************

浙公网安备 33010602011771号