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)?
There is 2 indexing in Python that points to the last element in the list. list[ len – 1 ] : This statement returns the last index if the list. list[-1] : Negative indexing starts from the end.
Using Python's "in" operator The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .
Exakt code used for timing: with open(file, "rb") as f: first = f. readline() # Read and store the first line. for last in f: pass # Read all lines, keep final value.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With