1
我得到试图读取一个JSON文件时出错:String对象有没有属性读取
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'
这是错误:
if os.path.exists(f) is True: ----> return json.loads(f.read()) else: return 'mocked.json' AttributeError: 'str' object has no attribute 'read'
什么我做错了任何帮助将不胜感激。
'”。加入()'导致串类型。所以你在一个字符串上执行'.read()' – Mangohero1
'f' - 你的字符串是路径,你不能在字符串 – Vladyslav