TypeError: write() argument must be str, not list (Python)

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: write() argument must be str, not list (Python) #

The Python "TypeError: write() argument must be str, not list" occurs when we try to write a list object to a file using the write() method. To solve the error, use the join() method to join the list into a string, e.g. my_file.write(','.join(my_list)).

typeerror write argument must be str not list

Here is an example of how the error occurs.

main.py
with open('example.txt', 'w', encoding='utf-8') as my_file: my_list = ['Alice', 'Bob', 'Carl'] # ⛔️ TypeError: write() argument must be str, not list my_file.write(my_list)
We passed a list object to the write() method, but the method can only be called with a string argument.

One way to solve the error is to use the str.join() method to join the list into a string.

main.py
with open('example.txt', 'w', encoding='utf-8') as my_file: my_list = ['Alice', 'Bob', 'Carl'] my_file.write(','.join(my_list)) # 👉️ "Alice,Bob,Carl"

We used a comma as the separator between the items of the list, but you can use any other separator or an empty string if you don't need a separator between the elements.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your list contains numbers, or other types, convert all of the values to string before calling join().

main.py
my_list = ['a', 'b', 1, 2] all_strings = list(map(str, my_list)) print(all_strings) # 👉️ ['a', 'b', '1', '2'] result = ''.join(all_strings) print(result) # 👉️ "ab12"

The string the method is called on is used as the separator between elements.

main.py
my_list = ['a', 'b', 'c'] my_str = '-'.join(my_list) print(my_str) # 👉️ "a-b-c"

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
my_list = ['a', 'b', 'c'] my_str = ''.join(my_list) print(my_str) # 👉️ "abc"

Alternatively, you can pass the list to the str() class to convert it to a string before writing it to the file.

main.py
with open('example.txt', 'w', encoding='utf-8') as my_file: my_list = ['Alice', 'Bob', 'Carl'] my_file.write(str(my_list)) # 👉️ ['Alice', 'Bob', 'Carl']

You can also convert the list to a JSON string before writing it to the file.

main.py
import json with open('example.txt', 'w', encoding='utf-8') as my_file: my_list = ['Alice', 'Bob', 'Carl'] my_file.write(json.dumps(my_list)) # 👉️ ["Alice", "Bob", "Carl"]

The json.dumps method converts a Python object to a JSON formatted string.

Conclusion #

The Python "TypeError: write() argument must be str, not list" occurs when we try to write a list object to a file using the write() method. To solve the error, use the join() method to join the list into a string, e.g. my_file.write(','.join(my_list)).

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.