“TypeError: write() argument must be str, not list” is a common error related to Python files when writing strings. The below explanations can help you know more about this error of causes and solutions.
How does the TypeError: write() argument must be str, not list occur?
Basically, “argument must be str’” means that the parameter or the instance you are passing to the method does not have the type of string type (in Python it is called str). Mostly, the error will happen when you call this method on a file and with the list passed to its parameter in Python. For example:
string = [' LEARNSHAREIT '] # Print the list print(string) # Write the list to the file with open('new.txt', 'w') as file: file.write(string)
Output in file new.txt
[' LEARNSHAREIT ']
Traceback (most recent call last):
File "main.py", line 8, in <module>
file.write(string)
TypeError: write() argument must be str, not list
The reason behind the lack of the str argument is that you may have mistakes in assuming the arguments in the write method will automatically convert the argument into string type. In fact, the method write() will not convert anything into a string, it just raises the error whenever the parameter is not str. The method print() and write() are most well-known in Python, however, the print() will auto-convert the arguments into strings for you, but the write() does not.
How to solve this error?
Using str()
Fortunately, Python has another method which can help you to convert a list into a string. You can read the syntax and usage of the str() method here. To use this method, just call it on your string variable, which is actually of type list rather than str:
# Create a list of str string = [' LEARNSHAREIT '] # Write the list to the file using str() with open('new.txt', 'w') as file: file.write(str(string))
Output in file new.txt
[' LEARNSHAREIT ']
The str() method produces a new string representing elements in the list separated by a comma. However, if you may want to replace commas with another character or remove separators, then you can read the next solution.
Using join
Another way to overcome this problem is to convert the list into a string with the commas replaced as another character by calling the method on the separator string you want:
# Declare the seperator as a semicolon separator = ';' # Declare our list of str strings = ['THIS', 'IS', 'LEARN,SHARE,IT'] # Write the list into the file using join() with open('new.txt', 'w') as file: file.write(separator.join(strings))
Output in file new.txt
THIS;IS;LEARN,SHARE,IT
Another example without creating a temporary separator variable:
# Declare the list of str strings = ['THIS', 'IS', 'LEARN,SHARE,IT'] # Write the list into the file using join() with open('new.txt', 'w') as file: file.write(''.join(strings))
Output in file new.txt
THISISLEARN,SHARE,IT
Summary
We have learned how to solve the TypeError: write() argument must be str, not list. By finding out the reasons and replacing the list with a string rather than a list with the help of third methods, you can easily avoid this error. Good luck to you!
Maybe you are interested:
- TypeError: object of type ‘float’ has no len() in Python
- TypeError: ‘str’ object cannot be interpreted as an integer
- TypeError: unhashable type: ‘set’ in Python
I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R