io.TextIOWrapper.readlines
Karen Shaeffer
klsshaeffer at icloud.com
Thu Nov 12 00:26:39 EST 2020
Hi folks,
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?
The builtin ‘open’ function is defined in the io streams module. I presume the builtin open(‘file’, ‘r’) returns an io.TextIOWrapper object. And maybe the readlines method just isn’t documented?
Just curious and lazy.
thanks,
Karen
More information about the Python-list
mailing list