Python读取配置文件(config.ini),写入配置文件

Posted 王张飞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python读取配置文件(config.ini),写入配置文件相关的知识,希望对你有一定的参考价值。

一、读取配置文件

说明: 上位机运行初始化时需要读取配置文件,使用所提方法,上位机运行过程中如果修改了参数,下次上位机启动可以直接读取上一次修改的参数进行配置。

实例:
我的目录如下(根目录下包括config_read.pyconfig_write.pyconfig.ini):

config.ini的内容如下:

[config]
platformname = Leovo
apppackage = com.romwe
appactivity = com.romwe.SplashActivity

[cmd]
viewphone = adb devices
startserver = adb start-server
stopserver = adb kill-server
install = adb install aaa.apk
id = 1
weight = 12.1
ischoice = True

[log]
log_error = true

[type]
stuno = 10211201

config_read.py中编写读取配置文件的脚本代码

import configparser
 
#  实例化configParser对象
config = configparser.ConfigParser()
# -read读取ini文件
config.read('C:\\\\Users\\\\songlihui\\\\PycharmProjects\\\\AutoTest_02\\\\config\\\\config.ini', encoding='GB18030')
# -sections得到所有的section,并以列表的形式返回
print('sections:' , ' ' , config.sections())
 
# -options(section)得到该section的所有option
print('options:' ,' ' , config.options('config'))
 
# -items(section)得到该section的所有键值对
print('items:' ,' ' ,config.items('cmd'))
 
# -get(section,option)得到section中option的值,返回为string类型
print('get:' ,' ' , config.get('cmd', 'startserver'))
 
# -getint(section,option)得到section中的option的值,返回为int类型
print('getint:' ,' ' ,config.getint('cmd', 'id'))
print('getfloat:' ,' ' , config.getfloat('cmd', 'weight'))
print('getboolean:' ,'  ', config.getboolean('cmd', 'isChoice'))
"""
首先得到配置文件的所有分组,然后根据分组逐一展示所有
"""
for sections in config.sections():
    for items in config.items(sections):
        print(items)

运行config_read.py文件,展示结果如下

sections:   ['config', 'cmd', 'log', 'type']
options:   ['platformname', 'apppackage', 'appactivity']
items:   [('viewphone', 'adb devices'), ('startserver', 'adb start-server'), ('stopserver', 'adb kill-server'), ('install', 'adb install aaa.apk'), ('id', '1'), ('weight', '12.1'), ('ischoice', 'True')]
get:   adb start-server
getint:   1
getfloat:   12.1
getboolean:    True
('platformname', 'Leovo')
('apppackage', 'com.romwe')
('appactivity', 'com.romwe.SplashActivity')
('viewphone', 'adb devices')
('startserver', 'adb start-server')
('stopserver', 'adb kill-server')
('install', 'adb install aaa.apk')
('id', '1')
('weight', '12.1')
('ischoice', 'True')
('log_error', 'true')
('stuno', '10211201')

config_write.py中编写读取配置文件的脚本代码

import configparser
 
#  实例化configParser对象
config = configparser.ConfigParser()
# -read读取ini文件
config.read('C:\\\\Users\\\\songlihui\\\\PycharmProjects\\\\AutoTest_02\\\\config\\\\config.ini', encoding='GB18030')
list = []
list = config.sections()# 获取到配置文件中所有分组名称
if 'type' not in list:# 如果分组type不存在则插入type分组
    config.add_section('type')
    config.set('type', 'stuno', '10211201')# 给type分组设置值
 
config.remove_option('type', 'stuno')# 删除type分组的stuno
config.remove_section('tpye')# 删除配置文件中type分组
o = open('C:\\\\Users\\\\songlihui\\\\PycharmProjects\\\\AutoTest_02\\\\config\\\\config.ini', 'w')
config.write(o)
o.close()#不要忘记关闭

运行config_write.py文件,展示结果如下

[config]
platformname = DELL
apppackage = com.romwe
appactivity = com.romwe.SplashActivity

[cmd]
viewphone = adb devices
startserver = adb start-server
stopserver = adb kill-server
install = adb install aaa.apk
id = 1
weight = 12.1
ischoice = True

[log]
log_error = true

[type]
stuno = 10211201

以上是关于Python读取配置文件(config.ini),写入配置文件的主要内容,如果未能解决你的问题,请参考以下文章

用python读取配置文件config.ini

python 读取ini 配置文件

python读取ini配置文件+Scala读取配置文件

python读取配置文件的方式

python读取ini配置文件

Python+Selenium进行UI自动化测试项目中,常用的小技巧2:读取配置文件(configparser,.ini文件)