tail(path, n) returning the last n lines of a file as a list (stripped of trailing newlines). For this problem a simple read-all approach is fine — but keep only the last n using slicing, and handle files shorter than n gracefully.tail('app.log', 2) # file has lines a, b, c['b', 'c']
The final two lines.
f.read().splitlines() drops the newlines for you.
A negative slice [-n:] takes the tail.