python configparser 模块

Ps:基于 Python3

创建配置文件

在项目根目录下创建 .ini 格式的配置文件:

touch conf.ini

配置文件大概长这样:

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

[bitbucket.org]
User = hg

[debug]
log_errors=true
show_warnings=False

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

优雅读取

from configparser

config = configparser.ConfigParser()

config.read("./conf.ini")
config.sections()
# 读取配置
config.get("DEFAULT", "Compression")
cfg.getboolean('debug','log_errors')
config.getint("topsecret.server.com", "Port")

参考资料