readlines and readline的区别

来源:互联网 发布:淘宝骑行头盔 编辑:程序博客网 时间:2024/05/22 19:26
#coding=utf-8#open a filenew = open('new.txt', 'r')print "The name of the file:%s" % newline = new.readlines()print "Read line1:%s" % (line)line = new.readlines()print "this is 2:", line   #前面已经读取出来了,所以这里是空的。new.close()#readlines(),一次性把每行读取出来,读取出来的格式是字符串来的。读取出来的很多字符串构成一个list.#

.readline() 和 .readlines() 之间的差异是后者一次读取整个文件,象 .read() 一样。.readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python 的 for … in … 结构进行处理。另一方面,.readline() 每次只读取一行,通常比 .readlines() 慢得多。仅当没有足够内存可以一次读取整个文件时,才应该使用 .readline()。

0 0