Read the bottom n lines of a file in Python

Using Operating System Commands in Python

Use the Linux/Mac tail command to provide the data:

line = subprocess.check_output(['tail', '-1', somefile.txt])

Creating a Python Only Script

Read the whole file into memory and only print out the last line/s:

with open('somefile.txt', 'r') as f:
    lines =
    last_line = lines[-1]

Leave a Reply

Your email address will not be published.