标签:python write writelines 文件操作 writelines() argument must be a sequence of strings
write和writelines的区别
1 write()需要传入一个字符串做为参数,否则会报错
2 writelines()既可以传入字符串又可以传入一个字符序列,并将该字符序列写入文件
3 注意必须传入的是字符序列,不能是数字序列(卡在这里搞了半天),刚开始测试时使用的是
list01 = [11,22,33,44,55]
报错:TypeError: writelines() argument must be a sequence of strings
测试write:
obj = open(‘log.py‘,‘r+‘)
list02 = ["11","test","hehe","44","55"]
obj.write(list02)
obj.close()
运行报错:TypeError: expected a character buffer object
测试writelines()
obj = open(‘log.py‘,‘r+‘)
list02 = ["11","test","hehe","44","55"]
obj.writelines(list02)
obj.close()
则执行成功。
文件内容如下:
11testhehe4455
本文出自 “linux飞龙在天” 博客,请务必保留此出处http://xiaotaoge.blog.51cto.com/1462401/1864761
标签:python write writelines 文件操作 writelines() argument must be a sequence of strings
原文地址:http://xiaotaoge.blog.51cto.com/1462401/1864761