configparser --- 設定檔剖析器

原始碼:Lib/configparser.py


This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what's found in Microsoft Windows INI files. You can use this to write Python programs which can be customized by end users easily.

備註

This library does not interpret or write the value-type prefixes used in the Windows Registry extended version of INI syntax.

也參考

tomllib 模組

TOML is a well-specified format for application configuration files. It is specifically designed to be an improved version of INI.

shlex 模組

Support for creating Unix shell-like mini-languages which can also be used for application configuration files.

json 模組

The json module implements a subset of JavaScript syntax which is sometimes used for configuration, but does not support comments.

Quick Start

Let's take a very basic configuration file that looks like this:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[forge.example]
User = hg

[topsecret.server.example]
Port = 50022
ForwardX11 = no

The structure of INI files is described in the following section. Essentially, the file consists of sections, each of which contains keys with values. configparser classes can read and write such files. Let's start by creating the above configuration file programmatically.

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config['DEFAULT'