Dev HelpersOpen library
← Back

Read only the final lines of a stream

Read only the final lines of a stream in Python.

Open this example in the interactive library

Source code

from collections import deque
from io import StringIO
last_lines = deque(StringIO('first\nsecond\nthird\n'), maxlen=2)
print(''.join(last_lines), end='')

Sample output

second
third