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

 

posted @ 2018-10-04 14:29  翻滚的小强  阅读(6162)  评论(0编辑  收藏  举报