【发布时间】:2019-03-05 20:20:11
【问题描述】:
我在尝试读取和解析 json 文件时收到错误 TypeError: expected str, bytes or os.PathLike object, not NoneType。我在网上检查了这个错误,并在stackoverflow上偶然发现了这个链接-
TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader
但上面看起来像同样的问题,但解决方案不适用于我的代码。
代码如下:
# 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)
追溯:
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
【问题讨论】:
-
@py_noob 你能告诉我们你是怎么打电话给
json_parser()的吗? -
fname1 = os.path.join(dirName, fname)