尝试解析JSON文件时不理解此TypeError

时间:2019-03-05 20:20:11

标签: python typeerror

当我尝试读取和解析json文件时,收到错误# function to parse the json file and extract useful information def json_parser(file): print(file) with open(file, 'r') as fp: json_decode = json.loads(fp.read()) pprint(json_decode) return json_decode # script to read through the directories and parse json files rootDir = 'projects/dataset' for dirName, subdirList, fileList in os.walk(rootDir): print("Found directory: %s" % dirName) for fname in fileList: print('\t%s' % fname) fname1 = print("'" + fname + "'") # calling the json parser function defined above result = json_parser(fname1) print(result) 。我在线检查了此错误,并在stackoverflow上偶然发现了此链接-

TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader

但是上面的内容看起来像是同样的问题,但是解决方案不适用于我的代码。

以下代码:

Error: File "test_json_parsing.py", line 15, in json_parser
    with open(file, 'r') as fp:
TypeError: expected str, bytes or os.PathLike object, not NoneType

跟踪:

> levels(meta$rating)
[1] "Conservative"

1 个答案:

答案 0 :(得分:1)

fname1 = print("'" + fname + "'")None分配给fname1,而不是打印的字符串。因此,如错误所示,fname1NoneType的对象,无法打开。

EDIT感谢@Jared Smith为我清除了此问题