Python_write和writelines的区别
Python_write和writelines的区别
1 write()需要传入一个字符串做为参数,否则会报错
2 writelines()既可以传入字符串又可以传入一个字符序列,并将该字符序列写入文件
注意 :writelines必须传入的是字符序列,不能是数字序列
如:list_1023 = [1,2,3,4,5]
报错:TypeError: write() argument must be str, not list
1、write():
fp = open("1023bb.txt","w+") l = ["1023","1024","1025"] fp.write(l) fp.seek(0,0) fp.read() fp.close()
运行报错:TypeError: write() argument must be str, not list
2、writelines()
fp = open("1023bb.txt","w+") l = ["1023","1024","1025"] fp.writelines(l) fp.seek(0,0) fp.read() fp.close()
则执行成功。
执行效果如下: