1. 程式人生 > >Python_異常:TypeError: write() argument must be str, not list

Python_異常:TypeError: write() argument must be str, not list

src txt -c file 技術分享 line ffffff code err

文件寫入操作時,報錯:TypeError: write() argument must be str, not list

原因:python寫入的內容要是字符串類型的

上代碼:

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()

寫入內容為字符串類型則ok

fp = open("a.txt","w")
fp.write([1,2,3])#將文件內容處理為字符串類型
fp.close()

技術分享圖片

Python_異常:TypeError: write() argument must be str, not list