AttributeError(“’str’ object has no attribute ‘read’”)

In Python I’m getting an error:

Exception:  (<type 'exceptions.AttributeError'>,
AttributeError("'str' object has no attribute 'read'",), <traceback object at 0x1543ab8>)

Given python code:

def getEntries (self, sub):
    url = ''
    if (sub != ''):
        url += 'r/' + sub
    request = urllib2.Request (url + 
        '.json', None, {'User-Agent' : 'Reddit desktop client by /user/RobinJ1995/'})
    response = urllib2.urlopen (request)
    jsonStr =
    return json.load(jsonStr)['data']['children']

What does this error mean and what did I do to cause it?


The problem is that for json.load you should pass a file like object with a read function defined. So either you use json.load(response) or json.loads(

Leave a Reply

Your email address will not be published. Required fields are marked *