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部分如下:
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包里面去找:
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)
大功告成, 哈
configparser读取配置文件时的相对路径问题的更多相关文章
- python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法
先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...
- python读取配置文件报keyerror-文件路径不正确导致的错误
- 在其他模块使用反射读取配置文件报错,但是在反射模块中读取GetData.check_list又是正确的 反射模块如下: # get_data.py from API_AUTO.p2p_projec ...
- ServletContext 接口读取配置文件要注意的路径问题
在建立一个maven项目时,我们通常把一些文件直接放在resource下面,在ServletContext中有getResource(String path)和getResourceAsStream( ...
- 记一次用python 的ConfigParser读取配置文件编码报错
记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...
- ConfigParser读取配置文件时报错:ConfigParser.MissingSectionHeaderError
使用ConfigParser来读取配置文件,经常会发现经过记事本.notepad++修改后的配置文件读取时出现下面的问题: ConfigParser.MissingSectionHeaderError ...
- Python读取文件时输入文件绝对路径报错
问题描述 读取本地文件,输入绝对路径时出错,显示数据被截断. 最开始以为是转译错误,因为eclipse会自动把\变成\\, pycharm这里没有,就手动改一下.结果还是不行. 根本原因 文件名出错了 ...
- python3使用configparser读取配置文件
python2中的ConfigParser在python3中改成了configparser 1.配置文件格式是 [域名] k=v 2.代码示例:需要生成conf.ini配置文件如下:[config]v ...
- 转载:Java项目读取配置文件时,FileNotFoundException 系统找不到指定的文件,System.getProperty("user.dir")的理解
唉,读取个文件,也就是在项目里面去获得配置文件的目录,然后,变成文件,有事没事,总是出个 FileNotFoundException 系统找不到指定的文件,气死人啦. 还有就是:System.get ...
- Python利用ConfigParser读取配置文件
http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...
随机推荐
- FCC---CSS Flexbox: Add Flex Superpowers to the Tweet Embed
To the right is the tweet embed that will be used as the practical example. Some of the elements wou ...
- cesium 圆圈警戒扫描(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- C lang:Pointer and multidimensional array
Xx_Introduction Double indrection:Address of Address;Pointer of Pointer Ax_Code #include<stdio.h& ...
- 挖掘Dark Sky Maps(热的要死后,疯传的一个气温地图网站)
最近,各种朋友圈,社会媒体,都在疯传一张图,这张图显示的全球的气温图,本没有什么特别的,但是这张图的网站来源所展示的数据与气象局或者各种天气预报的温度值相差倒是不少,引来一片网友的吐槽. 但是,作为专 ...
- 图片切换器(ImageSwitcher)的功能与用法
ImageSwitcher继承了ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换View组件时使用动画效果.ImageSwitcher继承了ViewSwitcher ...
- web项目踩坑过程
sql函数设计: 一开始本来是直接用Java的jdbc直接传输操作语句的.但后来学了存储过程发现存储过程可以提高不少的效率.就重构了自己对数据库的操作代码.包括:开启,查找,修改,关闭. 开启:直接使 ...
- leetcode题解:整数反转
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出 ...
- LeetCode 1291. 顺次数
地址 https://leetcode-cn.com/problems/sequential-digits/submissions/ 题目描述我们定义「顺次数」为:每一位上的数字都比前一位上的数字大 ...
- 洛谷 SP263 Period
洛谷 SP263 Period 题目描述 For each prefix of a given string S* with N* characters (each character has an ...
- vue 路由对象
路由对象在使用了 vue-router 的应用中,路由对象会被注入每个组件中,赋值为 this.$route ,并且当路由切换时,路由对象会被更新. so , 路由对象暴露了以下属性: 1.$rout ...