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