configparser读取配置文件时的相对路径问题

学习接口测试时,当我把配置文件xx.config和读取配置文件的模块read_config.py放在项目下的同一个包config里时,只需传入文件名xx.config便可实现对配置文件的读取. 可是当我在项目下另外一个包里导入read_config.py后,再次传入要读取的配置文件名xx.config,却报错了!学习

Traceback (most recent call last): File "C:\Users\wangyi\AppData\Local\Programs\Python\Python36\lib\configparser.py", line 1138, in _unify_values sectiondict = self._sections[section] KeyError: 'MOD' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/develop/lemon/tool/read_excel.py", line 88, in <module> button = ReadConfig().read_config('case.config','MOD','button') File "C:\develop\lemon\config\read_config.py", line 18, in read_config return cf.get(section,option) File "C:\Users\wangyi\AppData\Local\Programs\Python\Python36\lib\configparser.py", line 781, in get d = self._unify_values(section, vars) File "C:\Users\wangyi\AppData\Local\Programs\Python\Python36\lib\configparser.py", line 1141, in _unify_values raise NoSectionError(section) configparser.NoSectionError: No section: 'MOD'

纠结了半天,始终找不出错误缘由,想去百度一下,可是心想这么简单的问题难道我都解决不了还要去百度?那岂不是太没面子了呀! 我打开酷狗,放一首古琴曲, 试着让本身静下来,但窗外马路上的汽车鸣笛声让我静不下来,  忽然发现, 会不会是路径有问题?测试

read_config部分以下:spa

class ReadConfig: def read_config(self,file_name,section,option): cf = configparser.ConfigParser() cf.read(file_name,encoding='utf-8') return cf.get(section,option) if __name__ == '__main__': r = ReadConfig().read_config('case.config','MOD','button') print(r)

将寻找配置文件的路径改了一下,加了一行, 让你无论输入什么配置文件名都去config包里面去找:excel

class ReadConfig: def read_config(self,file_name,section,option):  file = os.path.abspath(os.path.join(os.getcwd(),'..','config',file_name)) cf = configparser.ConfigParser() cf.read(file,encoding='utf-8') return cf.get(section,option) if __name__ == '__main__': r = ReadConfig().read_config('case.config','MOD','button') print(r)

 

大功告成, 哈code

相关文章
相关标签/搜索