python:利用configparser模块读写配置文件
在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可。
这篇博客,介绍下python中利用configparser模块读写配置文件的方法,仅供参考。。。
一、读取文件
configparser模块支持读取.conf和.ini等类型的文件,那么首先在文件夹新建一个.ini文件,写入一些信息,如下图:

示例代码如下:
# coding=utf-8
import configparser
import os os.chdir("E:\\Automation\\UI\\testcase")
cf = configparser.ConfigParser() # read(filename) 读文件内容
filename = cf.read("test.ini")
print(filename) # sections() 得到所有的section,以列表形式返回
sec = cf.sections()
print(sec) # options(section) 得到section下的所有option
opt = cf.options("mysql")
print(opt) # items 得到section的所有键值对
value = cf.items("driver")
print(value) # get(section,option) 得到section中的option值,返回string/int类型的结果
mysql_host = cf.get("mysql","host")
mysql_password = cf.getint("mysql","password")
print(mysql_host,mysql_password)
执行脚本,结果如下所示:
['test.ini']
['driver', 'mysql']
['host', 'port', 'username', 'password']
[('path', 'E:\\Automation\\UI\\testcase\\browser\\chromedriver.exe'), ('url', 'https://www.baidu.com/')]
127.0.0.1 123456
脚本解析:
cf.read(filename):读取文件内容
cf.sections():得到所有的section,并且以列表形式返回
cf.options(section):得到section下所有的option
cf.items(option):得到该section所有的键值对
cf.get(section,option):得到section中option的值,返回string类型的结果
cf.getint(section,option):得到section中option的值,返回int类型的结果
二、写入文件
如果需要在配置文件写入内容,需要os函数帮忙,示例代码如下:
# coding=utf-8
import configparser
import os os.chdir("E:\\Automation\\UI\\testcase")
cf = configparser.ConfigParser() # 往配置文件写入内容 # add section 添加section项
# set(section,option,value) 给section项中写入键值对
cf.add_section("mq")
cf.set("mq", "user", "laozhang")
cf.add_section("kafka")
cf.set("kafka", "user", "xiaozhang") # write to file
with open("test1.ini","w+") as f:
cf.write(f)
执行脚本,结果如下所示:

脚本解析:
cf.write(filename):将configparser对象写入.ini类型的文件
add_section():添加一个新的section
add_set(section,option,value):对section中的option信息进行写入
三、修改文件
还可以利用os函数对文件进行修改,示例代码如下:
# coding=utf-8
import configparser
import os os.chdir("E:\\Automation\\UI\\testcase")
cf = configparser.ConfigParser() # 修改配置文件的内容 # remove_section(section) 删除某个section的数值
# remove_option(section,option) 删除某个section下的option的数值
cf.read("test1.ini")
cf.remove_option("kafka","user")
cf.remove_section("mq") # write to file
with open("test1.ini","w+") as f:
cf.write(f)
执行脚本,结果如下所示:

脚本解析:
cf.read(filename):读取文件(这里需要注意的是:一定要先读取文件,再进行修改)
cf.remove_section(section):删除文件中的某个section的数值
cf.remove_option(section,option):删除文件中某个section下的option的数值
如上所示,就是configparser模块读写配置文件的方法,代码仅为参考,具体使用请自行实践。。。
python:利用configparser模块读写配置文件的更多相关文章
- Python自动化测试 -ConfigParser模块读写配置文件
C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
- 用ConfigParser模块读写配置文件——Python
对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...
- Python自动化测试 (二) ConfigParser模块读写配置文件
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section, section 下有op ...
- Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件
前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...
- configparser模块读写ini配置文件
在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
随机推荐
- CentOS 安装 jdk
1.首下载CentOS对应的jdk压缩包. 2.通过secureCRT工具远程连接目标服务器. 3.通过rz命令上传jdk压缩包到linux服务器. 4.解压缩上传的jdk压缩包 tar -zxvf ...
- 新的 Centos 服务器初始化配置
当你初次创建新的 Centos 服务器的时候, Centos 默认的配置安全性和可用性上会存在一点缺陷(运维人员往往会有初始化的脚本).为了增强服务器的安全性和可用性,有些配置你应该尽快地完成. 这篇 ...
- SQL server 导出平面文件时出错: The code page on Destination - 3_txt.Inputs[Flat File Destination Input].Columns[UserId] is 936 and is required to be 1252.
我在导出平面文件时:Error 0xc00470d4: Data Flow Task 1: The code page on Destination - 3_txt.Inputs[Flat File ...
- Springboot helloworld入门最经典例子
一.建立maven java项目 导入springboot包 二.配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...
- MSSQL Sql加密函数 hashbytes 用法简介
转自:http://www.maomao365.com/?p=4732 一.mssql sql hashbytes 函数简介 hashbytes函数功能为:返回一个字符,通过 MD2.MD4.MD5. ...
- 探索SQL Server元数据(二)
背景 上一篇中,我介绍了SQL Server 允许访问数据库的元数据,为什么有元数据,如何使用元数据.这一篇中我会介绍如何进一步找到各种有价值的信息.以触发器为例,因为它们往往一起很多问题. 那么如何 ...
- 个人对于 Maven 的理解
个人对于 Maven 的理解 Maven 一直都在使用, 但如果说是不是真的懂 Maven, 很难谈得上. 或许什么时候系统地学习一下, 但在那之前, 打算先记下自己目前对于 Maven 的理解, 之 ...
- May 24. 2018 Week 21st Thursday
Man errs so long as he strives. 失误是进取的代价. It is not important that the man in the arena didn't win, ...
- 028实现strStr()
#pragma once #include "000库函数.h" /*********************自解**************/ //使用算法中的find 12ms ...
- Linux常用命令-解压缩篇
前言 Linux常用命令中,有很多用于对文件的压缩或解压,本文将介绍这些解压缩命令中不常见却非常实用的用法. tar tar是linux中最常用的解压缩命令.tar命令可用于处理后缀名为tar,tar ...