开源云真机平台-Sonic平台-python自定义脚本-config.ini方式实现全局配置参数的读写操作

【主要功能】

config.ini方式实现全局配置参数的读写操作

使用python实现以下功能:
1、使用将接口获取的变量值,写入到当前目录下的config文件中,如delayTime=10;
2、读取当前目录下的config文件中,特定变量的值,如delayTime=10;
3、若config文件或者节点不存在,则自动进行创建;

【详细代码】

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import configparser
import os


# 封装函数:写入配置文件
def write_config_ini(key, value, section="DEFAULT_SECTION", config_file='config.ini'):
    # 创建配置文件对象
    config = configparser.ConfigParser()

    # 若配置文件已存在,则读取原有内容
    if os.path.exists(config_file):
        config.read(config_file, encoding='utf-8')

    # 若配置文件不存在,则自动创建
    else:
        config.add_section(f'{section}')

    # 若section不存在,则创建新section
    if not config.has_section(section):
        config.add_section(f'{section}')


    # 设置变量值
    config[f'{section}'][key] = str(value)

    # 写入配置文件
    with open(config_file, 'w', encoding='utf-8') as f:
        config.write(f)

# 封装函数:一次写入多个值至配置文件,适用于大量数据写入,提高性能
def write_configs_ini(keys_values_dict, section="DEFAULT_SECTION", config_file='config.ini'):
    # 创建配置文件对象
    config = configparser.ConfigParser()

    # 若配置文件已存在,则读取原有内容
    if os.path.exists(config_file):
        config.read(config_file, encoding='utf-8')

    # 若配置文件不存在,则自动创建
    else:
        config.add_section(f'{section}')

    # 若section不存在,则创建新section
    if not config.has_section(section):
        config.add_section(f'{section}')

    # 设置变量值
    for key, value in keys_values_dict.items():
        config[f'{section}'][key] = str(value)

    # 写入配置文件
    with open(config_file, 'w', encoding='utf-8') as f:
        config.write(f)

# 封装函数:读取配置文件
def read_config_ini(key,  section="DEFAULT_SECTION",config_file='config.ini'):
    # 创建配置文件对象
    config = configparser.ConfigParser()

    # 若配置文件存在,则读取变量值
    if os.path.exists(config_file):
        config.read(config_file, encoding='utf-8')
        value = config[f'{section}'].get(key)
        return value

    # 若配置文件不存在,则返回空值
    else:
        return None


# 打印config文件的内容
def type_config(config_file='config.ini'):
    # 若配置文件存在,则读取所有变量值
    if os.path.exists(config_file):
        result = open(config_file, "r", encoding='utf-8').read()
        print(f"result={result}")
        return result

    # 若配置文件不存在,则返回空值
    else:
        return None


if __name__ == '__main__':
    # 示例:写入变量值到配置文件
    write_config_ini('delayTime', 98765,"HRD1")

    key_values = {'key1': "value1", 'key2': "value2"}
    write_configs_ini(key_values,"HRD1")

    type_config()

    # 示例:从配置文件中读取变量值
    value = read_config_ini('delayTime',"HRD1")
    print(f"read result={value}")

【运行效果】

result=[DEFAULT_SECTION]
delayTime = 98765
key1 = value1
key2 = value2

[HRD]
delayTime = 98765

[HRD1]
delayTime = 98765
key1 = value1
key2 = value2


read result=98765

实测,可以在不同的脚本中实现写操作和读操作,数据真实有效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

《代码爱好者》

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值