1
Python 之 configparser 模块详解和使用
作者:虫无涯
- 2023-03-28 陕西
本文字数:6322 字
阅读完需:约 21 分钟
1 configparser 安装
pip3 install configparser复制代码
2 configparser 简介
用来读取配置文件的 python 包;
一般做自动化测试的时候,会使用到这个模块,用来封装一些常量。比如数据库、邮件、用户名密码、项目常量等等;
这个使用根据个人喜好和项目来确定,不一定一定要使用这个模块,也可以使用其它的方法做配置,比如 py 文件、xml、excel、yaml、json 等等。
configparser 源码大约 1360 行左右,通读源码可有效了解该模块的使用。本文只做简单介绍常用的方法。
3 表示方法
新建一个名为
config.conf文件;写入如下数据,格式如下:
[mysqldb]sql_host = 127.0.0.1sql_port = 3699sql_user = rootsql_pass = 123456
[mailinfo]name = NoamaNelsonpasswd = 123456address = 123456@qq.com复制代码
4 configparser 详细使用
4.1 对象初始化
写入如下代码,把对象初始化。
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/11/19 # 文件名称:conf.py# 作用:configparser模块的使用# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelson
import configparserimport os
class Conf: def __init__(self): self.conf = configparser.ConfigParser() self.root_path = os.path.dirname(os.path.abspath(__file__)) self.f = os.path.join(self.root_path + "/config.conf") self.conf.read(self.f)
复制代码
4.2 获取所有的 sections
def read_sections(self): print(f"1、获取所有的sections:{self.conf.sections()}")
if __name__ == "__main__": aa = Conf() aa.read_sections()复制代码
结果为:
D:\Python37\python.exe F:/python_study/conf.py1、获取所有的sections:['mysqldb', 'mailinfo']
复制代码
4.3 获取所有的 sections 对应的 options
def read_options(self, s1, s2): print(f"2、获取mysqldb所有的options:{self.conf.options(s1)}") print(f"3、获取mailinfo所有的options:{self.conf.options(s2)}")
if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo")复制代码
结果为:
D:\Python37\python.exe F:/python_study/conf.py1、获取所有的sections:['mysqldb', 'mailinfo']2、获取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass']3、获取mailinfo所有的options:['name', 'passwd', 'address']复制代码
4.4 read 方法和 get 方法,获取指定 section 下的 option 值
read是读取配置文件,如self.conf.read(self.f)是读取指定config.conf文件;get是获取具体的值;
def read_conf(self, m, n): name = self.conf.get(m, n) # 获取指定section的option值 print(f"4、获取指定section:{m}下的option:{n}的值为{name}")
if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host")复制代码
结果为:
D:\Python37\python.exe F:/python_study/conf.py1、获取所有的sections:['mysqldb', 'mailinfo']2、获取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass']3、获取mailinfo所有的options:['name', 'passwd', 'address']4、获取指定section:mysqldb下的option:sql_host的值为127.0.0.1复制代码
4.5 items 方法,获取指点 section 所用配置信息
def get_items(self, m, n): print(f"5、获取sectoion:{m}下的配置信息为:{self.conf.items(m)}") print(f"6、获取sectoion:{n}下的配置信息为:{self.conf.items(n)}")
if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo")复制代码
结果为:
D:\Python37\python.exe F:/python_study/conf.py1、获取所有的sections:['mysqldb', 'mailinfo']2、获取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass']3、获取mailinfo所有的options:['name', 'passwd', 'address']4、获取指定section:mysqldb下的option:sql_host的值为127.0.0.15、获取sectoion:mysqldb下的配置信息为:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456')]6、获取sectoion:mailinfo下的配置信息为:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]
复制代码
4.6 set 和 write 方法,修改某个 option 的值
如果 option 不存在则创建它;
def set_option(self, m, n, s): self.conf.set(m, n, s) self.conf.write(open(self.f, "w")) print(f"7、设置setion:{m}下的option:{n}的值为:{s}")
if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客")
复制代码
结果为:
D:\Python37\python.exe F:/python_study/conf.py1、获取所有的sections:['mysqldb', 'mailinfo']2、获取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass']3、获取mailinfo所有的options:['name', 'passwd', 'address']4、获取指定section:mysqldb下的option:sql_host的值为127.0.0.15、获取sectoion:mysqldb下的配置信息为:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456')]6、获取sectoion:mailinfo下的配置信息为:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]7、设置setion:mysqldb下的option:sql_name的值为:游客
复制代码
因为之前的配置文件中没有
sql_name,所以会新创建它,如果创建的内容存在,直接修改对应的值;
4.7 has_section 和 has_option 方法
has_section方法检查对应的section是否存在;has_option方法检查对应的option是否存在;
def has_s_o(self, s, o): print(f"8、检查section:{s}是否存在:{self.conf.has_section(s)}") print(f"9、检查section:{s}下的option:{o}是否存在:{self.conf.has_option(s, o)}")
if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客") aa.has_s_o("mysqldb", "sql_name")
复制代码
结果为:
D:\Python37\python.exe F:/python_study/conf.py1、获取所有的sections:['mysqldb', 'mailinfo']2、获取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass', 'sql_name']3、获取mailinfo所有的options:['name', 'passwd', 'address']4、获取指定section:mysqldb下的option:sql_host的值为127.0.0.15、获取sectoion:mysqldb下的配置信息为:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456'), ('sql_name', '游客')]6、获取sectoion:mailinfo下的配置信息为:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]7、设置setion:mysqldb下的option:sql_name的值为:游客8、检查section:mysqldb是否存在:True9、检查section:mysqldb下的option:sql_name是否存在:True
复制代码
4.8 add_section 方法,添加 section 和 option
add_section:添加section;添加前:
def add_s_o(self, s, o, v): if not self.conf.has_section(s): self.conf.add_section(s) print(f"10、添加新的section为{s}") else: print(f"10、添加新的section为{s}已经存在,无需添加!") if not self.conf.has_option(s, o): self.conf.set(s, o, v) print(f"11、要添加的option为{o}, 值为{v}") else: print(f"11、要添加的option为{o}, 值为{v},已经存在,无需添加!") self.conf.write(open(self.f, "w"))
if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客") aa.has_s_o("mysqldb", "sql_name") aa.add_s_o("login", "name", "root")
复制代码
添加后:
D:\Python37\python.exe F:/python_study/conf.py1、获取所有的sections:['mysqldb', 'mailinfo']2、获取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass', 'sql_name']3、获取mailinfo所有的options:['name', 'passwd', 'address']4、获取指定section:mysqldb下的option:sql_host的值为127.0.0.15、获取sectoion:mysqldb下的配置信息为:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456'), ('sql_name', '游客')]6、获取sectoion:mailinfo下的配置信息为:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]7、设置setion:mysqldb下的option:sql_name的值为:游客8、检查section:mysqldb是否存在:True9、检查section:mysqldb下的option:sql_name是否存在:True10、添加新的section为login11、要添加的option为name, 值为root
复制代码
再次运行代码,会提示已经存在:
10、添加新的section为login已经存在,无需添加!11、要添加的option为name, 值为root,已经存在,无需添加!
复制代码
4.9 remove_section 和 remove_option 方法,删除 section 和 option
def remove_s_o(self, s, o): if self.conf.has_section(s): self.conf.remove_section(s) print(f"12、删除section:{s}==OK!") else: print(f"12、要删除的section:{s}不存在,不用删除!") if self.conf.has_option(s, o): self.conf.remove_option(s, o) print(f"13、删除section:{s}下的option:{o}==OK!") else: print(f"13、要删除的section:{s}下的option:{o}不存在,不用删除!")
if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客") aa.has_s_o("mysqldb", "sql_name") aa.add_s_o("login", "name", "root") aa.remove_s_o("login", "name")
复制代码
结果为:
D:\Python37\python.exe F:/python_study/conf.py1、获取所有的sections:['mysqldb', 'mailinfo', 'login']2、获取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass', 'sql_name']3、获取mailinfo所有的options:['name', 'passwd', 'address']4、获取指定section:mysqldb下的option:sql_host的值为127.0.0.15、获取sectoion:mysqldb下的配置信息为:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456'), ('sql_name', '游客')]6、获取sectoion:mailinfo下的配置信息为:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]7、设置setion:mysqldb下的option:sql_name的值为:游客8、检查section:mysqldb是否存在:True9、检查section:mysqldb下的option:sql_name是否存在:True10、添加新的section为login已经存在,无需添加!11、要添加的option为name, 值为root,已经存在,无需添加!12、删除section:login==OK!13、要删除的section:login下的option:name不存在,不用删除!
复制代码
5 configparser 常见的异常
6 本文所有源码
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/11/19 # 文件名称:conf.py# 作用:configparser模块的使用# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelson
import configparserimport os
class Conf: def __init__(self): self.conf = configparser.ConfigParser() self.root_path = os.path.dirname(os.path.abspath(__file__)) self.f = os.path.join(self.root_path + "/config.conf") self.conf.read(self.f)
def read_sections(self): print(f"1、获取所有的sections:{self.conf.sections()}")
def read_options(self, s1, s2): print(f"2、获取mysqldb所有的options:{self.conf.options(s1)}") print(f"3、获取mailinfo所有的options:{self.conf.options(s2)}")
def read_conf(self, m, n): name = self.conf.get(m, n) # 获取指定section的option值 print(f"4、获取指定section:{m}下的option:{n}的值为{name}")
def get_items(self, m, n): print(f"5、获取sectoion:{m}下的配置信息为:{self.conf.items(m)}") print(f"6、获取sectoion:{n}下的配置信息为:{self.conf.items(n)}")
def set_option(self, m, n, s): self.conf.set(m, n, s) self.conf.write(open(self.f, "w")) print(f"7、设置setion:{m}下的option:{n}的值为:{s}")
def has_s_o(self, s, o): print(f"8、检查section:{s}是否存在:{self.conf.has_section(s)}") print(f"9、检查section:{s}下的option:{o}是否存在:{self.conf.has_option(s, o)}")
def add_s_o(self, s, o, v): if not self.conf.has_section(s): self.conf.add_section(s) print(f"10、添加新的section为{s}") else: print(f"10、添加新的section为{s}已经存在,无需添加!") if not self.conf.has_option(s, o): self.conf.set(s, o, v) print(f"11、要添加的option为{o}, 值为{v}") else: print(f"11、要添加的option为{o}, 值为{v},已经存在,无需添加!") self.conf.write(open(self.f, "w"))
def remove_s_o(self, s, o): if self.conf.has_section(s): self.conf.remove_section(s) print(f"12、删除section:{s}==OK!") else: print(f"12、要删除的section:{s}不存在,不用删除!") if self.conf.has_option(s, o): self.conf.remove_option(s, o) print(f"13、删除section:{s}下的option:{o}==OK!") else: print(f"13、要删除的section:{s}下的option:{o}不存在,不用删除!")
if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客") aa.has_s_o("mysqldb", "sql_name") aa.add_s_o("login", "name", "root") aa.remove_s_o("login", "name")复制代码
划线
评论
复制
发布于: 刚刚阅读数: 4
版权声明: 本文为 InfoQ 作者【虫无涯】的原创文章。
原文链接:【http://xie.infoq.cn/article/68839bf0565c7d9540fa06578】。文章转载请联系作者。
虫无涯
关注
专注测试领域各种技术研究、分享和交流~ 2019-12-11 加入
CSDN测试领域优质创作者 | CSDN博客专家 | 阿里云专家博主 | 华为云享专家 | 51CTO专家博主










评论