‘str’ object has no attribute ‘read’ ( Solved )


The error attributeerror: ‘str’ object has no attribute ‘read’ occurs when you try to read the string file from the filename instead of the file object. If you have a file that contains JSON response and you use the json.load() method then you will also get the ‘str’ object has no attribute ‘read’ error.

In this entire tutorial, you will know how to solve this attributeError.

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

In most of the cases ‘str’ object has no attribute ‘read’ error comes due to the two cases. You will know each case with its solution in this section.

Case 1: Using read() method on filename

Suppose you have a file with the name “text_file.txt” and you want to read and display it on the screen. The first step is to open the filename using the file stream and then read the file. Most of the developers instead of using the file object then call the read() method on the filename.

Look at the example given below. You will get the error ‘str’ object has no attribute ‘read’ error when you will run it.

text_file = "sample_Text.txt"
with open(text_file, encoding='utf-8') as f:
    read = text_file.read()
    print(read)

Output

str object has not attribute read error
str object has not attribute read error

Solution

In the above example, you can see I am calling the read() method using the filename(text_file) not on the file object(f). Thtas why you are getting the error. You will not get the attributeError when you will call the read() method on the file object.

text_file = "sample_Text.txt"
with open(text_file, encoding='utf-8') as f:
    read = f.read()
    print(read)

Output

Solving str object has not attribute read error for the text file
Solving str object has not attribute read error for the text file

Case 2: Using json.load() method

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.

import json
json_response = '{"website_name":"Data Science Learner"}'
res = json.load(json_response)

Output

str object has not attribute read error while using json.read() method
str object has not attribute read error while using json.read() method

Solution

The solution for the above case is that you have to use the json.loads() method to read the JSON response from the string type. Now You will not get the error.

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

Output

Reading json response from the string
Reading json response from the string

You have to first read 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.

Conclusion

The attributeerror: ‘str’ object has no attribute ‘read’ error is mostly due to not properly calling the read() method. The read() method must be called using the file object, not the filename.

The above method will solve the error you are getting due to the above cases.

I hope you have liked this tutorial. Still, if you have any doubt then you can contact us for more information.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.





Source link