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


文件寫入操作時,報錯: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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM