# -*- coding: utf-8 -*-
from ConfigParser import ConfigParser
import import os
conf_parser = ConfigParser()
conf_parser.read([os.path.join(os.path.dirname(__file__), "default.conf"),
os.path.join(os.path.dirname(__file__), "produce.conf")])
kafka_servers = _conf_parser.get("meta", "kafka-servers")
print kafka_servers
default.conf内容:
[meta]
kafka-servers=10.101.12.4:9092
produce.conf内容:
[meta]
kafka-servers=99999999999:00000
说明:os.path.dirname(__file__)获取当前代码执行文件所在目录 ,join相当于拼接下级路径。
假设:os.path.dirname(__file__)=/home/until
os.path.join(os.path.dirname(__file__), "default.conf")=/home/until/default.conf
我上面的代码意思是,default.conf 和produce.conf 是在同级目录。
conf_parser.read() 接受一个文件数组,,,且后面加载的配置文件的配置项会覆盖前面加载的同名的配置项目。
配置文件格式:INI 格式
14. python 生产环境配置文件覆盖测试环境配置文件
于 2020-11-26 11:40:10 首次发布