python操作配置文件ini的方法
2018-01-11 12:41
686 查看
在写测试脚本时,经常有一些需要变动的数据,可以单独放在ini文件里,然后读取传递给
相应的函数,这样程序操作更灵活。具体的方法介绍如下:
文件结构:
![](https://img-blog.csdn.net/20180111124020127?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbmllZG9uZ3Jp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Cofig.ini内容:
直接上代码:
import
configparser
conf = configparser.ConfigParser()
conf.read("cofig.ini")
#读取配置文件里所有的Section
print(conf.sections())
#打印出test1这个section下包含key
print(conf.options("test1"))
#打印test1这个section下所有的key及对应的values
print(conf.items("test1"))
conf.add_section("add")#添加section到配置文件
conf.set("add","ip","11.11.1.1"<
4000
span style="color:#FFFFFF;">)#add
section新增ip参数和值
conf.set("add","addr","shenzhen")
conf.write(open("cofig.ini","w"))#写完数据要write一下
print(conf.items("add"))#打印刚添加的新内容
输出的结果:
['test1', 'test2', 'test3']
['ip']
[('ip', '10.10.10.10')]
[('ip', '11.11.1.1'), ('addr', 'shenzhen')]
相应的函数,这样程序操作更灵活。具体的方法介绍如下:
文件结构:
Cofig.ini内容:
[test1] ip = 10.10.10.10 [test2] port = 25566 [test3] name = www.baidu.com
直接上代码:
import
configparser
conf = configparser.ConfigParser()
conf.read("cofig.ini")
#读取配置文件里所有的Section
print(conf.sections())
#打印出test1这个section下包含key
print(conf.options("test1"))
#打印test1这个section下所有的key及对应的values
print(conf.items("test1"))
conf.add_section("add")#添加section到配置文件
conf.set("add","ip","11.11.1.1"<
4000
span style="color:#FFFFFF;">)#add
section新增ip参数和值
conf.set("add","addr","shenzhen")
conf.write(open("cofig.ini","w"))#写完数据要write一下
print(conf.items("add"))#打印刚添加的新内容
输出的结果:
['test1', 'test2', 'test3']
['ip']
[('ip', '10.10.10.10')]
[('ip', '11.11.1.1'), ('addr', 'shenzhen')]
相关文章推荐
- python对配置文件.ini进行增删改查操作的方法示例
- python读写ini配置文件方法实例分析
- python 3 ini配置文件操作
- PHP中操作ini配置文件的方法
- Python3读写INI配置文件简单方法
- Python使用ConfigParser模块操作配置文件的方法
- Python模块configparser(操作配置文件ini)
- python读写ini配置文件方法实例分析
- PHP中操作ini配置文件的方法
- python3配置文件ini读取方法
- python读取/写入配置文件ini方法
- Python实现读写INI配置文件的方法示例
- python 提供INI配置文件的操作 ConfigParser
- C# 关于 ini 配置文件的操作
- Delphi TIniFile 类 操作ini配置文件
- 配置文件INI的操作
- 用vb.net读取INI配置文件的方法,使用API
- Delphi TIniFile 类 操作ini配置文件
- 用VC操作ini配置文件
- Python读写配置文件的实际操作步骤解析