[issue968063] Add fileinput.islastline()

Serhiy Storchaka report at bugs.python.org
Mon Feb 6 01:15:55 EST 2017


Serhiy Storchaka added the comment:

The patch is outdated. It uses private _buffer attribute which no longer exist (see issue15068). But even when _buffer was existing the patch was not correct, because _buffer contained only a part of the file.

It is possible to implement islastline(), but at the cost of additional buffering. This effectively negates the result of issue15068. The user would need to enter two lines first than fileinput could return the fist one from stdin. And additional complication slows down reading for all users even if they don't need islastline().

If you need islastline() you can use a wrapper:

def yield_line_and_islastline(f):
    try:
        prevline = next(f)
    except StopIteration:
        return
    for line in f:
        yield (prevline, f.isfirstline())
        prevline = line
    yield prevline, True

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue968063>
_______________________________________


More information about the Python-bugs-list mailing list