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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "AttributeError: 'tuple' object has no attribute 'write'" occurs when we try to call the write() method on a tuple. To solve the error, open() the file before writing to it, e.g. with open('example.txt', 'w') as my_file.

attributeerror tuple object has no attribute write

Here is an example of how the error occurs.

main.py
my_file = ('example.txt', 'w') # ⛔️ AttributeError: 'tuple' object has no attribute 'write' my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n') my_file.close()

The my_file variable stores a tuple because we have multiple, comma-separated elements.

Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

To solve the error in this scenario, we have to open the file before writing to it.

main.py
my_file = open('example.txt', '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()

We used the open() function to open the file before writing to it.

Note that using with is better because it takes care of closing the file automatically, even if an exception is thrown.

main.py
with open('example.txt', 'w', 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 append data to a file, use the a flag instead of w.

main.py
# 👇️ appends data to the file with open('example.txt', '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
with open('example.txt', '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')

The reason we got the "AttributeError: 'tuple' object has no attribute 'write'" initially is because we created a tuple by mistake.

Tuples are constructed in multiple ways:

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

Note that tuples are immutable, so if you have to mutate a sequence, you have to use a list instead.

There are only 2 methods that you will likely be using on tuple objects.

main.py
my_tuple = ('a', 'b', 'c', 'c') print(my_tuple.count('c')) # 👉️ 2 print(my_tuple.index('a')) # 👉️ 0

The count method returns the number of occurrences of the value in the tuple and the index method returns the index of the value in the tuple.

You can view all the attributes an object has by using the dir() function.

main.py
my_tuple = ('a', 'b', 'c', 'c') # 👇️ [... 'count', 'index' ...] print(dir(my_tuple))

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: tuple object has no attribute" error.

Conclusion #

The Python "AttributeError: 'tuple' object has no attribute 'write'" occurs when we try to call the write() method on a tuple. To solve the error, open() the file before writing to it, e.g. with open('example.txt', 'w') as my_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.