attributeerror: ‘str’ object has no attribute ‘read’ [SOLVED]

In this tutorial, we will provide solutions for attributeerror: ‘str’ object has no attribute ‘read’ error.

Apart from it, we will discover the causes and have a brief discussion of this error.

What is attributeerror: ‘str’ object has no attribute ‘read’?

The AttributeError: ‘str’ object has no attribute ‘read’ error message typically occurs in Python when trying to use the read() method on a string object.

The read() method is a file object method in Python that allows you to read the contents of a file.

However, when you try to use it on a string object, you will get the “AttributeError” message because the read() method is not defined for string objects.

Causes of attributeerror: ‘str’ object has no attribute ‘read’?

Here are a few common scenarios that can lead to this error:

  • Trying to read from a string variable
    • If you have a string variable and you try to call the read() method on it, you’ll get this error.
  • Confusing a string object with a file object
    • If you intend to read from a file and accidentally pass a string object instead of a file object to a function that expects a file object, you’ll get this error.
  • Typo in method name
    • If you misspell the method name as reed() instead of read(), Python will not recognize it as a valid method and you’ll get an AttributeError.

Solutions to fix attributeerror: ‘str’ object has no attribute ‘read’

Here are the following solutions to try in fixing attributeerror: ‘str’ object has no attribute ‘read’ error:

Use read() method

For instance, we have a file named my_file.text and we want it to read and display on the screen.

myfile_loc= "my_file.txt"

data = myfile_loc.read()

As we can see the myfile_loc is a string, so when we call the read() function on file it will raise an error.

Traceback (most recent call last):
  File ...
    data = file.read()
AttributeError: 'str' object has no attribute 'read'

To fix this error we should open the file first and we will call the read() function from the returned object:

Take a look at the example below:

myfile_loc= "output.txt"

with open(myfile_loc, "r") as file:
    data = file.read()
    print(data)

Therefore, the code above will not give an error because read() function is being called from the file object instead of the string myfile_loc.

Output:

output

Pass a string to json.load()

The other case when you will get the attributeerror: ‘str’ object has no attribute ‘read’ error is when you are using the json.load() while parsing the json response.

Take a look at how the error occurs:

import json
json_response = '{"website_name":"Itsourcecode"}'
res = json.load(json_response)

AttributeError: ‘str’ object has no attribute ‘read’

To fix this error we should use the json.loads() method enables to read JSON from the string type.

import json
json_response = '{"website_name":"Itsourcecode"}'
res = json.loads(json_response)
print(res)

Output:

{‘website_name’: ‘Itsourcecode’}

Moreover, we need read first the JSON file before parsing the JSON response using the json.load() method.

It is only for that case when you have a JSON response saved on the file.

Call read on urllib module

We may encounter this error when we incorrectly call the read function from a URL string while we are using the urllib module.

For instance, we try to read the content of a website as follows:

import urllib.request

url = 'https://google.com/'

data = url.read()

Output:

AttributeError: ‘str’ object has no attribute ‘read’

The right way to read a website’s content is to open first the url with urllib.request.urlopen() like this:

import urllib.request

with urllib.request.urlopen('https://google.com/') as file:
    data = file.read()
    print(data)

Take a look at the print() function output to verify the fix.

Output:

output urllib

Conclusion

In conclusion, the error Attributeerror: ‘str’ object has no attribute ‘read’ occurs when the read() function is called from a string rather than a file object.

Numerous times, we’ve misunderstood how to access and read a file object, such as when calling json.load() or reading from a URL. The given solution above should assist you to resolve this error.

We hope you’ve learned a lot from this.

If you are finding solutions to some errors you might encounter we also have Typeerror: nonetype object is not callable.

Thank you for reading 🙂

Leave a Comment