# coding:utf-8 import configparser import os import json base_path = os.path.dirname(os.path.abspath(__file__)) conf_file= os.path.join(base_path,"config.ini") def read_config(): #读取配置文件信息 try: cf=configparser.ConfigParser() cf.read(conf_file,encoding="utf-8") section_data=dict() for section in cf.sections(): section_data[section] ={} for k,v in cf.items(section): section_data[section][k] = v return section_data except Exception as e: print("读取配置文件[{}]异常:{}".format(conf_file,e)) return {} if __name__ == "__main__": print(json.dumps(read_config(),indent=" "))
标签:__,配置文件,python,section,cf,file,path,configparser From: https://www.cnblogs.com/boye169/p/17485629.html