配置文件格式:
config.ini
内容:
[xxx]
ip = 123.456.789
port = 9986
方法:
def read_config_file(name, section):
"""
读取配置文件信息
:param name: 配置文件路径
:param section: section
:return: 返回对应的section的字典
"""
import configparser
config_dict = {}
config = configparser.ConfigParser()
config.read(name, encoding="utf-8")
if section in config.sections():
for (key, value) in config.items(section):
config_dict[key] = value
return config_dict
else:
logger.error('section not in %s file.' % name)
return config_dict
config_path = Path(__file__).parent.parent.parent.absolute() / 'config.ini'
result = read_config_file(config_path, xxx)
ip = result["ip"]