On 11Nov2020 21:26, Karen Shaeffer <klsshaef...@icloud.com> wrote: >import io > >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. > > >>>> help(io.TextIOWrapper.readlines) >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. Test it: >>> open('/dev/null','r') <_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 have handy): A buffered text stream over a BufferedIOBase binary stream. It inherits TextIOBase. 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: >>> open('/dev/null','r').readlines <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. Cheers, Cameron Simpson <c...@cskk.id.au> -- https://mail.python.org/mailman/listinfo/python-list