Reputation: 2984
Suppose I want to process each line of a file, but the last line needs special treatment:
with open('my_file.txt') as f:
for line in f:
if <line is the last line>:
handle_last_line(line)
else:
handle_line(line)
The question is, how does one implement ? There seems to be no function for detecting end-of-file in Python.
Is there another solution than read the lines into a list (with f.readlines() or similar)?
Upvotes: 6
Views: 2590
Reputation: 1122172
Process the previous line:
with open('my_file.txt') as f:
line = None
previous = next(f, None)
for line in f:
handle_line(previous)
previous = line
if previous is not None:
handle_last_line(previous)
When the loop terminates, you know that the last line was just read.
A generic version, letting you process the N last lines separately, use a collections.deque()
object:
from collections import deque
from itertools import islice
with open('my_file.txt') as f:
prev = deque(islice(f, n), n)
for line in f:
handle_line(prev.popleft())
prev.append(line)
for remaining in prev:
handle_last_line(remaining)
Upvotes: 15
Reputation: 101959
You can use itertools.tee
to iterate on two copies of an iterable:
next_lines, lines = itertools.tee(file_object)
next(next_lines)
for next_line, line in zip(next_lines, lines):
handle_line(line)
last_line = next(lines, None)
if last_line is not None:
handle_last_line(last_line)
Upvotes: 3