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

时间:2018-10-04 14:56:02   收藏:0   阅读:6728

文件写入操作时,报错: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()

技术分享图片

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!