On 11Nov2020 21:26, Karen Shaeffer <klsshaeffer at icloud.com> wrote:
>with io.open(filename, ?r?) as fd:
> lines = fd.readlines(hint=1000)
> for line in lines:
> # do something
>I was just looking at the io module. io.open(?file?, ?r') returns an io.TextIOWrapper object, which has the io.TextIOWrapper.readlines(hint=-1/) method.
>readlines(self, hint=-1, /)
> Return a list of lines from the stream.
> hint can be specified to control the number of lines read: no more
> lines will be read if the total size (in bytes/characters) of all
> lines so far exceeds hint.
>I haven?t verified this, but that looks like it isn?t reading the entire file. With hint=1000, the method returns as many complete lines that consume less than 1000 bytes of the stream. I?m lazy. Didn?t test it. Seems like only 1000 bytes would be read from the file, rather than the entire file?
Test it. To my eye, I also think that is what it means.
>The builtin ?open? function is defined in the io streams module. I presume the builtin open(?file?, ?r?) returns an io.TextIOWrapper object.
<_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
> And maybe the readlines method just isn?t documented?
It is documented though.
The docs for io.TextIOWrapper start with this (from the 3.8.0 docs I
A buffered text stream over a BufferedIOBase binary stream. It
The docs for io.TextIOBase start with this:
Base class for text streams. This class provides a character and
line based interface to stream I/O. It inherits IOBase.
And the docs for IOBase has the readlines method with the signature you
describe from its help.
Nte that if I look at the readlines from TextIOWrapper it says:
<built-in method readlines of _io.TextIOWrapper object at 0x10b90ae10>
which probably means TextIOWrapper is a C implementation (for
performance), not doing the plain old class inheritance you'd get from a
pure Python implementation.
Cameron Simpson <cs at cskk.id.au>