AttributeError:读取JSON文件时,“ str”对象没有属性“ read”

时间:2018-07-05 02:17:57

标签: json python-3.x

这是我要实现的目标: 我正在尝试检查目录并列出其所有文件夹和文件。如果有一个文件夹,我想列出该文件夹中的所有文件(它们都是JSON格式),然后,我想打开每个JSON文件并对其执行功能。但是我收到以下错误,似乎与JSON有关,有人可以告诉我我做错了吗?

return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

这是我的代码:

import os
from os import listdir
import glob
import json
list_dir=[]
all_files=[]
read_file=[]

for r,d,f in os.walk(r'C:\aschemas-master'):  #show only directories along with path
   for dirs in d:
     filepath = os.path.join(r, dirs)
     list_dir.append(filepath)
for folders in list_dir:       
    all_files=os.listdir(folders)  
    for each_file in all_files:
        file_read=os.path.join(folders+'\\'+each_file)
        x=open(file_read)
        file_write=os.path.join(folders+'\\'+each_file+"_madh"+".txt")
        w=open(''.join(file_write),'w')
        **json_load=json.load(x.read())**
        for x in get_dotted_form("", json_load):  # calls the function
            w.write(x)
            w.write('\n')
        w.close()

1 个答案:

答案 0 :(得分:0)

功劳归@AChampion。我的一个文件夹中有一些txt文件!