AttributeError: 'str' object has no attribute 'write'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'str' object has no attribute 'write' #

The Python "AttributeError: 'str' object has no attribute 'write'" occurs when we call the write() method on a string (e.g. the filename) instead of a file object. To solve the error, call the write() method on the file object after opening the file.

attributeerror str object has no attribute write

Here is an example of how the error occurs.

main.py
file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as my_file: # ⛔️ AttributeError: 'str' object has no attribute 'write' file_name.write('first line' + '\n') file_name.write('second line' + '\n') file_name.write('third line' + '\n')

The issue is that we called the write() method on the filename, which is a string.

Instead, call the write() method on the file object after you open it.

main.py
file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as my_file: # ✅ calling write() on file object my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n')

The with open() syntax takes care of automatically closing the file even if an exception is thrown.

Alternatively, you can store the file object into a variable and manually close it.

main.py
file_name = 'example.txt' my_file = open(file_name, 'w', encoding='utf-8') my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n') my_file.close()

Note that it's better to use the with open() syntax as it automatically closes the file after we are done.

If you need to append data to a file, use the a flag instead of w.

main.py
file_name = 'example.txt' with open(file_name, 'a', encoding='utf-8') as my_file: my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n')

If you need to read from the file and write to the file, use the r+ flag.

main.py
file_name = 'example.txt' with open(file_name, 'r+', encoding='utf-8') as my_file: read_data = my_file.read() print(read_data) my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n')

A good way to start debugging is to print(dir(your_object)) and see what attributes a string has.

Here is an example of what printing the attributes of a string looks like.

main.py
my_string = 'hello world' # [ 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', # 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', # 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', # 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', # 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', # 'title', 'translate', 'upper', 'zfill'] print(dir(my_string))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: str object has no attribute error".

Since the str object doesn't implement a write() method, the error is caused.

Conclusion #

The Python "AttributeError: 'str' object has no attribute 'write'" occurs when we call the write() method on a string (e.g. the filename) instead of a file object. To solve the error, call the write() method on the file object after opening the file.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.