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

When writing to a file, an error is reported: TypeError: write() argument must be str, not list

Reason: The content written by python should be of string type

Code.

fp = open(“a.txt”,”w”)
fp.write([1,2,3])
fp.close()

>>> fp = open("a.txt","w")
>>> fp.write([1,2,3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not list
>>> fp.close()

If the writing content is string type, OK

fp = open("a.txt","w")
fp.write('[1,2,3]')#Processing file contents to string type
fp.close()

Similar Posts: