使用python 读写 ini config 文件
Published in:2023-11-06 |
Words: 557 | Reading time: 3min | reading:

使用python 读写 ini config 文件

使用configparser 库读写ini文件,ini配置文件由节、键、值组成;【参数】(键=值):INI所包含的最基本的“元素”就是参数(parameter),每个参数都有一个name和一个value,name和value由等号“=”隔开,name在等号的左边

编写

  • ini文件在程序开发中经常使用,具体形式如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    [minio_config_selected]
    ;注释 section name 节 name = value
    minio_config_id = 3245678
    minio_server_ip = 11.22.10.12
    minio_server_port = 9000
    minio_account = 1321
    minio_password = 12312
    mark_msg = wewe
    enable = 1

读写

  • 读取ini文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36

    @logger.catch
    def read_ini_config(file_name, section, value_key):
    """
    读取指定配置文件
    :param file_name: ini file name
    :param section: ini file section name
    :param value_key: ini file content name
    :return: select value
    """
    # file_name = "../config/config.ini"

    # Writing Data
    config = configparser.ConfigParser()

    config.read(file_name, encoding="utf-8")
    keys = [
    "host",
    "user",
    "port",
    "password",
    "port"
    ]
    # for key in keys:
    try:
    value = config.get(section, value_key)
    # logger.info("read ini file :" + file_name + ", ini file config content : " + config.get(section, value_key))
    return value
    except configparser.NoSectionError as e:
    # logger.info("normal")
    logger.error("Error! section: " + section + ", value_key :" + value_key + ", value error content: " + str(e))
    return "log_dir"
    except configparser.NoOptionError:
    logger.error(f"No option '{section}' in section , please re input config key''" + value_key)
    return ""

  • 2.写入配置内容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    @logger.catch
    def write_minio_config_to_file(minio_config):
    """
    ini 配置文件写入
    :param minio_config: 元组 notice
    :return:
    """
    iniPath = os.path.realpath(ini_file_path) # 读取生成后运行时的临时文件目录
    logger.info("generate file path:" + iniPath)
    conf = configparser.ConfigParser()
    if os.path.exists(iniPath):
    os.remove(iniPath)
    logger.warning("Not Found config ini file , creating ini file ....")
    if not os.path.exists(ini_path):
    os.makedirs(ini_path)
    logger.debug("dir not exists ,create dir")
    # tes = open(iniPath, 'a+')
    # tes.close()
    conf.read(iniPath, 'utf-8')
    logger.info("start generate config ini file :")

    conf.add_section("minio_config_selected")
    conf.set("minio_config_selected", "minio_config_id", minio_config['minio_config_id'])
    conf.set("minio_config_selected", "minio_server_ip", minio_config['minio_server_ip']) # 写入配置参数
    conf.set("minio_config_selected", "minio_server_port", str(minio_config['minio_server_port']))
    conf.set("minio_config_selected", "minio_account", minio_config['minio_account'])
    conf.set("minio_config_selected", "minio_password", minio_config['minio_password'])
    conf.set("minio_config_selected", "mark_msg", minio_config['mark_msg'])
    conf.set("minio_config_selected", "enable", '1')
    # conf.set(minio_config.minio_config_id, "minio_server_ip", minio_config.minio_server_ip)
    # tes.write(conf.values())
    conf.write(open(iniPath, 'a+', encoding="utf-8"))
    conf.read(iniPath, 'utf-8')
    logger.info("config write finished , read test : " + conf.get("minio_config_selected", "minio_server_ip"))
Prev:
基于Assammdf第三方python库处理mdf、mf4文件
Next:
基于Spring Cloud应用activiti流程框架