![](https://img.haomeiwen.com/i5959551/8124209c03c1176c.png)
无论做自动化测试还是测试开发或者其它工作中,肯定需要做一些配置并在程序中读取这些预设置好的配置内容,那么最常用的无疑是ini配置文件了
本文首发于伊洛的个人博客:https://yiluotalk.com,欢迎关注并查看更多内容!!!
1. ini配置文件
- ini配置文件内容由,节,键(或者称为选项),值三部分构成
[section_1]
option_1 = value_1
option_2 = value_2
[section_2]
- 大小写敏感
- 另外有一个特殊的节(
section
),就是默认节(DEFAULT
)
2. python
读取配置文件
- 首先我们写一个配置文件并预设内容
# 伊洛Yiluo
# 自动化测试配置文件
[test]
# 测试人
tester = yiluo
# 测试环境
environment = test
# 测试版本
versionCode = 1.0.0
host = https://test.yiluotalk.com
[release]
tester = luoyi
environment = release
versionCode = 2.0.0
# 正式域名(伊洛的个人博客)
host = https://yiluotalk.com
- 使用
ConfigParser
模块,并实例化
In [1]: from configparser import ConfigParser
In [2]: config = ConfigParser()
- 返回所有的节点
In [3]: config.read('test.ini')
Out[3]: ['test.ini']
In [10]: config.sections()
Out[10]: ['test', 'release']
- 查询
section
(节点)是否存在
In [7]: config.has_section('test')
Out[7]: True
In [12]: config.has_section('demo')
Out[12]: False
- 返回指定节点(
section
)里面的所有选项
In [14]: config.options('test')
Out[14]: ['tester', 'environment', 'versioncode', 'host']
- 获取
伊洛
正式环境个人博客
的地址
In [16]: config.get('release', 'host')
Out[16]: 'https://yiluotalk.com'
- 其余可能常用到的还有
config.set/config.write/config.add_section
欢迎下方【戳一下】【点赞】
Author:伊洛Yiluo
愿你享受每一天,Just Enjoy !
网友评论