import configparser
from API.Common import path
class DoConfigparser():
def __init__(self):
self.cf = configparser.ConfigParser()
self.cf.read(path.global_file,encoding ="utf-8") # 加载全局配置文件,文件地址是根据os模块配置的相对路径, 里面有中文加载时一定要指明编码格式
statu = self.cf.getboolean("switch","open")
if statu == False:
self.cf.read(path.test_file, encoding="utf-8")
else:
self.cf.read(path.online_file, encoding="utf-8")
def get_value(self,section,option):
return self.cf.get(section,option)
handles = DoConfigparser()
if __name__ == '__main__':
URL = DoConfigparser().get_value("url","url")
print(URL)
这里是配置文件的格式
![](https://img2020.cnblogs.com/blog/1771964/202008/1771964-20200805142426065-1786022824.png)