导入 configparser
from configparser import ConfigParser
1、创建配置解析器对象
config = ConfigParser()
2、构造要写入的数据
# 写入配置的时候,不用使用read
# 嵌套字典的字典
datas = { "file path":{ "case_path":"testcase.xlsx", "log_path":"record_run_result.txt" }, "msg":{ "success_result":"Pass", "fail_result":"Fail" } }
循环读取
# config["区域名"]["选项名"] for key in datas: config[key] = datas[key] # config 相当于一个空字典
3、保存到文件
with open("write_config_ini",'w') as file: config.write(file)
执行之后的write_config.ini 文件
[file path] case_path = testcase.xlsx log_path = record_run_result.txt [msg] success_result = Pass fail_result = Fail