要修改ini文件,需要使用Python内置的configparser模块。以下是一个示例:

import configparser

# 创建config对象,读取ini文件
config = configparser.ConfigParser()
config.read('example.ini')

# 修改section中的option
config.set('Section1', 'option1', 'new_value')

# 添加一个新的section
config.add_section('Section3')
config.set('Section3', 'option1', 'value1')

# 保存修改后的ini文件
with open('example.ini', 'w') as f:
    config.write(f)

其中,config.read('example.ini')读取ini文件,config.set('Section1', 'option1', 'new_value')修改section1中的option1的值,config.add_section('Section3')添加一个新的section,config.set('Section3', 'option1', 'value1')在新的section中添加option1的值。最后,使用config.write(f)保存修改后的ini文件

python 修改ini文件

原文地址: https://www.cveoy.top/t/topic/fUpI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录