# AttributeError: ‘Str’ Object Has No Attribute ‘Write’ – How To Fix It?

If you encounter the AttributeError: ‘str’ object has no attribute ‘write’ in Python. You can fix this error by replacing it with the file object ‘f’ or using the open() function.

## What causes the AttributeError: ‘str’ object has no attribute ‘write’?

The AttributeError: ‘str’ object has no attribute ‘write’ happens when you call write() on a string.

For example:

I created a file to write data to named ‘demo1.txt’.

pathFile = 'demo1.txt'
strMesg = 'Hello! Visit learnshareit website'

with open(pathFile, mode='w') as f:
pathFile.write(strMesg)

Output:

Traceback (most recent call last):
File "d:\vscode\Python\hello.py", line 5, in <module>
pathFile.write(strMesg)
AttributeError: 'str' object has no attribute 'write'

I open the file with the ‘with’ statement in the above example. A little introduction about the ‘with’ statement for you to easily visualize the error. The ‘with’ command has the following structure:

with open(filepath, mode, encoding=None) as f:
processing command block

Parameters:

• open: is the open() function to open a file.
• f: is the file object returned if the file was opened successfully.

The ‘with’ statement will automatically close the file, which is an advantage over opening the file with the open() function.

Syntax of the write() function:

f.write(str)

Parameters:

• f: is the file object created when opening the file with the open() function.
• str: is the string of characters you want to overwrite into the file.

## How to solve the AttributeError: ‘str’ object has no attribute ‘write’?

### Change the name of the file as the string to the file object

I have clearly shown you the syntax of the functions, parameters, and the cause of the error; this is how I fixed the error.

Example:

• Make sure you have created a file to write data. In this example, I named it ‘demo1.txt’.
• Create a string to write to file ‘demo1.txt’.
• Replace the name of the file as the string ‘pathFile’ with the name of the file object ‘f’.
pathFile = 'demo1.txt'
strMesg = 'Hello! Visit learnshareit website.'

# Replace the file's name as the string 'pathFile' with the file object 'f'
with open(pathFile, mode='w') as f:
f.write(strMesg)

with open(pathFile) as f:
print('Data taken from file demo1:', dataInFile)

Output:

Data taken from file demo1: Hello! Visit learnshareit website.

Note:

• The AttributeError: ‘str’ object has no attribute ‘read’ can also occur when you confuse the file’s name and the file object.
• The write() function accepts only string type.

### Open the file with the open() function

If you feel that opening the file with the with statement is difficult, you should use the open() function to open the file, although it is not as convenient as the easy-to-remember syntax. Then write data to the file usually.

Example:

• Make sure you have created a file to write data. In this example, I named it ‘demo1.txt’.
• Write data to file ‘demo1’ using the write() function.
• When done, remember to close the file with the command: f.close().
# Use the open() function to open a file
f = open("demo1.txt", "w")

# Use the write() function to write data to the file
f.write("Hello! Visit learnshareit website.")
f.close()

# Open the file to see the results
f = open("demo1.txt", "r")
print(f.read())

Output:

Hello! Visit learnshareit website.

## Summary

I hope you have an idea to fix the AttributeError: ‘str’ object has no attribute ‘write’ in Python. In my opinion, you should open the file with the open() function to fix the error.

Maybe you are interested: