配置文件是把公共的常量放到配置文件,进行读取,可以放在.ini文件或者通过yml读取
fromconfigparserimportConfigParser
importyamlclassConfigHandler(ConfigParser):def__init__(self,file,encoding=utf-8):"""参数:-file:配置文件名的路径"""#config=ConfigParser()super().__init__()self.read(file,encoding=encoding)defread_yaml(file,encoding=utf-8):withopen(file,encoding=encoding)asf:returnyaml.load(f.read(),Loader=yaml.FullLoader)defwrite_yaml(file,data,encoding=utf-8):"""写入yaml"""withopen(file,encoding=encoding,mode=w)asf:yaml.dump(data,stream=f,allow_unicode=True)if__name__==__main__:##读取#config=ConfigHandler(python1.ini)#a=config.get(teachers,name)#print(a)#aprint(read_yaml(python1.yaml))#先读取yaml数据#data=read_yaml(python1.yaml)#write_yaml(python2.yaml,data)#获取config=ConfigHandler(python1.ini)print(config[teachers])config[teachers][name]=benbenwithopen(python1.ini,a,encoding=utf-8)asf:a=config.write(f)ini文件格式
[teachers]name=hyb[teachers]name=benben
yaml文件格式
request:
method:post
logger:level:DEBUG预览时标签不可点收录于话题#个上一篇下一篇