杰瑞科技汇

Python configparser 如何读写配置文件?

Of course! The configparser module in Python is a powerful and standard way to handle configuration files. It's particularly well-suited for INI-style configuration files, which are human-readable and commonly used in applications and systems.

Python configparser 如何读写配置文件?-图1
(图片来源网络,侵删)

Here's a comprehensive guide covering everything from basic usage to advanced features.

What is configparser?

It's a built-in Python module (since Python 3.2) that parses configuration files structured into sections with key-value pairs. This structure is similar to the classic Windows INI files.

Example INI file structure (config.ini):

[DEFAULT]
# This section provides default values for all other sections
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no

Reading a Configuration File

This is the most common use case. You'll read a file and access its values.

Python configparser 如何读写配置文件?-图2
(图片来源网络,侵删)

Step-by-Step Example

Let's use the config.ini file from above.

import configparser
# 1. Create a ConfigParser object
config = configparser.ConfigParser()
# 2. Read the configuration file
#    The 'read' method can take a single filename or a list of filenames.
#    It returns a list of successfully read files.
try:
    config.read('config.ini')
    print("Successfully read config.ini")
except FileNotFoundError:
    print("Error: config.ini not found.")
# 3. Accessing data
# The 'get' method retrieves values. It automatically handles type conversion.
# For boolean values, it understands 'yes', 'no', 'true', 'false', 'on', 'off'.
# For integers, it converts the string to an int.
# Accessing a value from the DEFAULT section
print(f"\nDefault Compression: {config.get('DEFAULT', 'Compression')}") # Output: yes
print(f"Default Compression Level: {config.getint('DEFAULT', 'CompressionLevel')}") # Output: 9
# Accessing a value from a specific section
print(f"\nBitbucket User: {config.get('bitbucket.org', 'User')}") # Output: hg
# Accessing a value from another section
print(f"TopSecret Server Port: {config.getint('topsecret.server.com', 'Port')}") # Output: 50022
# You can also use the DEFAULT section's values in other sections
print(f"TopSecret ServerAliveInterval: {config.getint('topsecret.server.com', 'ServerAliveInterval')}") # Output: 45 (inherited from DEFAULT)

Important Methods for Reading Data:

  • config.get(section, key): Returns the value as a string.
  • config.getint(section, key): Returns the value as an integer.
  • config.getfloat(section, key): Returns the value as a float.
  • config.getboolean(section, key): Returns the value as a boolean. It's smart and understands yes/no, true/false, on/off, 1/0.

Writing to a Configuration File

You can also create and write configuration files. The ConfigParser object maintains an in-memory representation of the configuration, which you can then save to a file.

import configparser
# 1. Create a new ConfigParser object
config = configparser.ConfigParser()
# 2. Add sections and key-value pairs
# You can add sections and then set values within them.
config['Database'] = {
    'host': 'localhost',
    'port': '5432',
    'user': 'admin',
    'password': 'secret',
    'use_ssl': 'yes'
}
config['Logging'] = {
    'level': 'INFO',
    'file_path': '/var/log/myapp.log'
}
# 3. Write the configuration to a file
# The 'write' method requires a file object opened in write mode ('w').
with open('new_config.ini', 'w') as configfile:
    config.write(configfile)
print("Successfully wrote new_config.ini")

The resulting new_config.ini file will look like this:

[Database]
host = localhost
port = 5432
user = admin
password = secret
use_ssl = yes
[Logging]
level = INFO
file_path = /var/log/myapp.log

Modifying an Existing Configuration

You can read a file, modify its values in memory, and then write it back.

Python configparser 如何读写配置文件?-图3
(图片来源网络,侵删)
import configparser
# Read the existing config file
config = configparser.ConfigParser()
config.read('config.ini')
# Modify a value
config['bitbucket.org']['User'] = 'my_new_user' # Direct assignment
config.set('topsecret.server.com', 'Port', '2222') # Using the set() method is safer
# Add a new key-value pair to an existing section
config['topsecret.server.com']['Timeout'] = '30'
# Add a completely new section
config['new_section'] = {
    'key1': 'value1',
    'key2': 'value2'
}
# Write the changes back to the file
with open('config.ini', 'w') as configfile:
    config.write(configfile)
print("Successfully modified config.ini")

Advanced Features

Checking for Keys and Sections

Before accessing a key, it's good practice to check if it exists to avoid KeyError exceptions.

if 'bitbucket.org' in config:
    print("The bitbucket.org section exists.")
if 'User' in config['bitbucket.org']:
    print("The 'User' key exists in the bitbucket.org section.")
# A more Pythonic way is to use the .get() method with a default value
# This returns the default value if the key is not found.
user = config.get('bitbucket.org', 'User', fallback='anonymous')
print(f"User: {user}")

Iterating Over Sections and Keys

You can easily loop through the configuration.

# Print all section names
print("Sections:", config.sections()) # Output: ['bitbucket.org', 'topsecret.server.com', 'new_section']
# Iterate through sections and their keys
for section_name in config.sections():
    print(f"\nSection: [{section_name}]")
    for key, value in config.items(section_name):
        # Note: config.items() includes items from the DEFAULT section
        print(f"  {key} = {value}")

Handling Interpolation (Advanced)

ConfigParser can perform variable interpolation. This means you can use one variable's value to define another. This is controlled by the interpolation parameter.

  • configparser.BasicInterpolation: The default. Uses %(key)s syntax.
  • configparser.ExtendedInterpolation: Uses ${section:key} syntax, which is more explicit.

Example with BasicInterpolation:

Let's create a template.ini file:

[Connection]
host = server.com
port = 8080
url = http://%(host)s:%(port)s/api

Now, let's read it:

import configparser
config = configparser.ConfigParser(interpolation=configparser.BasicInterpolation())
config.read('template.ini')
# The 'url' key is automatically interpolated
print(config['Connection']['url'])
# Output: http://server.com:8080/api

Best Practices and Common Pitfalls

  1. Use try...except FileNotFoundError: Always wrap config.read() in a try-except block to handle cases where the configuration file doesn't exist.
  2. Use Type-Specific Getters: Use getint(), getfloat(), and getboolean() instead of manually converting get() results. It's cleaner and less error-prone.
  3. Provide Default Values: Use the fallback argument in get() to gracefully handle missing configuration options. This makes your code more robust.
  4. File Permissions: When writing a configuration file, ensure your application has the necessary write permissions in the target directory.
  5. File Order: config.read() can accept a list of filenames. It will read them in order, and settings in later files will override settings in earlier ones. This is useful for overriding default configs with environment-specific ones.
分享:
扫描分享到社交APP
上一篇
下一篇