可选参数应表示从文件中读取多少(大约)个字节.该文件将被进一步读取,直到当前行结束:
readlines([size]) -> list of strings, each a line from the file.
Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.
另一个引用:
If given an optional parameter sizehint, it reads that many bytes from the file and enough more to complete a line, and returns the lines from that.
你是对的,它似乎对小文件没有太大作用,这很有趣:
In [1]: open('hello').readlines()
Out[1]: ['Hello\n', 'there\n', '!\n']
In [2]: open('hello').readlines(2)
Out[2]: ['Hello\n', 'there\n', '!\n']
有人可能认为文档中的以下短语解释了它:
Read until EOF using readline() and return a list containing the lines thus read. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read. Objects implementing a file-like interface may choose to ignore sizehint if it cannot be implemented, or cannot be implemented efficiently.
但是,即使我尝试在没有缓冲的情况下读取文件,它似乎也没有改变任何东西,这意味着其他类型的内部缓冲区意味着:
In [4]: open('hello', 'r', 0).readlines(2)
Out[4]: ['Hello\n', 'there\n', '!\n']
在我的系统上,这个内部缓冲区大小似乎是大约5k字节/ 1.7k行:
In [1]: len(open('hello', 'r', 0).readlines(5))
Out[1]: 1756
In [2]: len(open('hello', 'r', 0).readlines())
Out[2]: 28080