槑槑匠

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

python写入配置文件

from configparser import ConfigParser

def write_into_config(config_path, section, option, value):
    config = ConfigParser()
    config.read(config_path)
    sections = config.sections()
    if section in sections:
        config.remove_section(section)
    config.add_section(section)
    config.set_section(section, option, value)
    fo = open(config_path, 'w', encoding='UTF-8')  # 重新创建配置文件
    config.write(fo)  # 数据写入配置文件
    fo.close()
posted on 2020-04-30 09:43  槑槑匠  阅读(381)  评论(0)    收藏  举报