what happens when the file begin read is too big for all lines to be read with "readlines()"
steve at REMOVETHIScyber.com.au
Sun Nov 20 06:26:34 CET 2005
On Sun, 20 Nov 2005 16:10:58 +1100, Steven D'Aprano wrote:
> def get_line(filename, token):
> """Returns the next line following a token, or None if not found.
> Leading and trailing whitespace is ignored when looking for
> the token.
> fp = file(filename, "r")
> for line in fp:
> if line.strip() == token:
> # runs only if we didn't break
> print "Token not found"
> result = None
> result = fp.readline() # read the next line only
> return result
Correction: checking the Library Reference, I find that this is
wrong. The reason is that file objects implement their own read-ahead
buffer, and mixing calls to next() and readline() may not work right.
Replace the fp.readline() with fp.next() and all should be good.
More information about the Python-list