Python's function readlines(n) behavior

后端 未结 3 2086
半阙折子戏
半阙折子戏 2020-12-06 06:15

I\'ve read the documentation, but what does readlines(n) do? By readlines(n), I mean readlines(3) or any other number.

When I run readlines(3), it returns same thing

3条回答
  •  旧巷少年郎
    2020-12-06 06:32

    The optional argument should mean how many (approximately) bytes are read from the file. The file will be read further, until the current line ends:

    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.
    

    Another quote:

    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.

    You're right that it doesn't seem to do much for small files, which is interesting:

    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']
    

    One might think it's explained by the following phrase in the documentation:

    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.

    However, even when I try to read the file without buffering, it doesn't seem to change anything, which means some other kind of internal buffer is meant:

    In [4]: open('hello', 'r', 0).readlines(2)
    Out[4]: ['Hello\n', 'there\n', '!\n']
    

    On my system, this internal buffer size seems to be around 5k bytes / 1.7k lines:

    In [1]: len(open('hello', 'r', 0).readlines(5))
    Out[1]: 1756
    
    In [2]: len(open('hello', 'r', 0).readlines())
    Out[2]: 28080
    

提交回复
热议问题