Fundamentals 5 min read

Using Python ConfigParser to Read, Write, and Manage INI Files

This article introduces Python's built-in ConfigParser library, demonstrating how to read, write, update, handle multiple, and dynamically generate sections and options in INI configuration files through five practical code examples for interface automation tasks.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Python ConfigParser to Read, Write, and Manage INI Files

When working on interface automation, configuration files are common and important. Python provides a powerful standard library, ConfigParser, for parsing and handling INI files. This article shows how to use ConfigParser to read and manipulate INI files with five practical examples.

01 Read configuration file

import configparser
# 创建 ConfigParser 对象
config = configparser.ConfigParser()
# 读取配置文件
config.read('config.ini')
# 获取配置项的值
base_url = config.get('API', 'base_url')
api_key = config.get('API', 'api_key')
print("Base URL:", base_url)
print("API Key:", api_key)

02 Write configuration file

import configparser
# 创建 ConfigParser 对象
config = configparser.ConfigParser()
# 添加配置项和值
config['API'] = {'base_url': 'https://api.example.com',
                 'api_key': 'your-api-key'}
# 写入配置文件
with open('config.ini', 'w') as configfile:
    config.write(configfile)
print("配置文件写入成功!")

03 Update configuration value

import configparser
# 创建 ConfigParser 对象
config = configparser.ConfigParser()
# 读取配置文件
config.read('config.ini')
# 更新配置项的值
config.set('API', 'api_key', 'new-api-key')
# 写入更新后的配置文件
with open('config.ini', 'w') as configfile:
    config.write(configfile)
print("配置项的值已更新!")

04 Handle multiple configuration files

import configparser
# 创建 ConfigParser 对象
config = configparser.ConfigParser()
# 读取多个配置文件
config.read(['config.ini', 'config_override.ini'])
# 获取配置项的值(优先使用 override 文件中的配置)
base_url = config.get('API', 'base_url')
api_key = config.get('API', 'api_key')
print("Base URL:", base_url)
print("API Key:", api_key)

05 Dynamically generate sections and options

import configparser
# 创建 ConfigParser 对象
config = configparser.ConfigParser()
# 动态生成节和选项
sections = ['API', 'Database']
options = {'API': ['base_url', 'api_key'],
           'Database': ['host', 'username', 'password']}
for section in sections:
    config.add_section(section)
    for option in options[section]:
        value = input(f"请输入 {option} 的值:")
        config.set(section, option, value)
# 写入配置文件
with open('config.ini', 'w') as configfile:
    config.write(configfile)
print("配置文件已生成!")

We introduced the basic operations of using ConfigParser to parse INI files and provided five real-world code examples for interface automation. By applying these snippets, you can easily read, write, update, handle multiple configuration files, and dynamically generate sections and options.

We hope this article helps you better understand and apply Python's ConfigParser library for handling INI files. If you are interested in interface automation, this tool will be a valuable assistant.

backendPythonAutomationConfigurationiniConfigParser
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.