【发布时间】:2017-08-31 13:20:09
【问题描述】:
我在尝试读取 JSON 文件时遇到错误:
def get_mocked_json(self, filename):
f = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../fixtures', filename)
if os.path.exists(f) is True:
return json.loads(f.read())
else:
return 'mocked.json'
这是错误:
如果 os.path.exists(f) 为真: ----> 返回 json.loads(f.read()) 别的: 返回“嘲笑.json” AttributeError:“str”对象没有属性“read”对我做错的任何帮助将不胜感激。
【问题讨论】:
-
' '.join()产生字符串类型。所以你在一个字符串上执行.read() -
f- 是你的带路径的字符串,你不能在字符串上调用read()方法