Borislav Hadzhiev
Wed Apr 20 2022·2 min read
Photo by Nadia H.
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.
Here is an example of how the error occurs.
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.
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.
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
.
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.
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.
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 classes' attributes, and recursively of the attributes of its bases.
Since the str
object doesn't implement a write()
method, the error is
caused.