from configparser import ConfigParser class HandleConfig: """ 封装配置文件类 """ def __init__(self, filename): self.filename = filename self.config = ConfigParser self.config.read(self.filename, encoding='utf-8') def get_config(self, section, option): """ 获取配置文件 :return: """ return self.config.get(section, option) def get_int(self, section, option): """ Get int data type : return: "" " return self.config.getint (Section, the Option) DEF get_float (Self, Section, the Option): " "" get float type of data : param Section: : param the Option: : return : "" " return self.config.getfloat (Section, the Option) DEF get_boolean (Self, Section, the Option): " "" get bool type of data : param Section: : param the Option: : return: "" " return Self. config.getboolean (Section, the Option) DEF get_eval_value (Self, section, option): "" " Get python built-in types : param Section: : param the Option: : return: " "" return eval (self.get_config (Section, the Option)) @classmethod DEF write_config (Self, DATAS, filename): "" " write profile : param DATAS: : param filename: : return: "" " # determine whether a nested dictionary dictionary iF isinstance (DATAS, dict): for value in datas.values (): iF not isinstance (value, dict) : return config = ConfigParser for key in datas: config[key] =datas[key] with open(filename, 'w')as file: config.write(file)