How To Fix Error “AttributeError: ‘Str’ Object Has No Attribute ‘Read'”

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

If you are getting trouble with the error “AttributeError: ‘str’ object has no attribute ‘read’”, don’t worry, then follow our article to overcome the problem.

Reason for the AttributeError: ‘str’ object has no attribute ‘read’

The essence of the problem is that you try to access methods or properties of a string object with no attribute read. User often get the error due to 2 mostly reasons below.

Reason 1: Read a file in a wrong manner

When you intend to read the content of a text file:

Welcome to LikeShareIT
Have a nice day!

Code:

file = "a.txt"
file.read()

Result

Traceback (most recent call last):
  File "code.py", line 2, in <module>
    file.read()
AttributeError: 'str' object has no attribute 'read'

The error “‘str’ object has no attribute’read'” informs us that the attribute read is absent from the string object. The read() function returns the provided number of bytes from the file and is a member of the File data type.

Reason 2: Misunderstand when loading a json file

The json library has two different load() methods for File objects and string objects. Thus you can get this error when using it. Read more json APIs here.

json.load(): parse the JSON file

json.loads(): parse the JSON string

Look at the following example of Json file: a.json:

{"student":[
    { "firstName":"John", "lastName":"Doe" },
    { "firstName":"Anna", "lastName":"Smith" },
    { "firstName":"Peter", "lastName":"Jones" }
]}

Code:

import json
  
file = open('/content/drive/MyDrive/LearnShareIT/a.json', 'r') 
line = file.read()
print(json.load(line))
file.close()

Result:

Traceback (most recent call last):
  File "code.py", line 5, in <module>
  print(json.load(line))
AttributeError: 'str' object has no attribute 'read'

Solutions to solve this problem?

Solution 1: Use the correct command when reading the file

You must use the correct command when reading the file to solve this problem. Look at the following syntax of it:

Syntax:

var_name = open(“path_to_file”, “r”)

Code:

file = open("a.txt", "r")
print(file.read())

Result:

Welcome to LikeShareIT
Have a nice day!

Solution 2: Distinguish clearly the function of loading method JSON

Excepting the above solution, you must clearly distinguish the function of the loading method JSON.

Here is json.load() method:

Code:

import json
 
file = open('a.json', 'r')
content = json.load(file)
print(content)
 
file.close()

Result:

{'student': [{'firstName': 'John', 'lastName': 'Doe'}, {'firstName': 'Anna', 'lastName': 'Smith'}, {'firstName': 'Peter', 'lastName': 'Jones'}]}

And this is an example of json.loads() method:

Code:

import json
 
file = open('a.json', 'r')
content = json.loads(file.read())
for line in content['student']:
    print(line)

file.close()

Result:

{'firstName': 'John', 'lastName': 'Doe'}
{'firstName': 'Anna', 'lastName': 'Smith'}
{'firstName': 'Peter', 'lastName': 'Jones'}

Summary

AttributeError: ‘str’ object has no attribute ‘read’ is a common error when reading files. Throughout our article, we hope you understand the reason for the problem and work with files wisely to avoid the mistakes.

Maybe you are interested in similar errors:

Leave a Reply

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