Python - Read the last few lines of file

By xngo on July 28, 2019

In Python, there are multiple ways to read the last few lines of a file. Some are easy, convenient or efficient and some are not.

Small file

For small file, you can load the whole file into memory and access the last 5 lines using negative indices. Here is the example.

# Load the whole file into memory.
with open("myfile.txt", "r") as file:            
    lines=file.readlines()
 
# Use negative indices to print last 5 lines.
#   Will display the last line, 2nd line from the end,
#       3rd line from the end and so on.
for i in range(1,6):
    print(lines[i*-1])

Big file

Obviously, for big file, you can't load the whole file into memory. Otherwise, you may run out of memory space. So, a better way would be to read through the file line by line and hold only the last 5 lines in a list until the end. Here is an example.

# Read line by line and hold the last 5 lines in last_lines list.
with open("myfile.txt", "r") as file:
    i=0
    lines_size = 5
    last_lines = []
    for line in file:
        if i < lines_size:
            last_lines.append(line)
        else:
            last_lines[i%lines_size] = line
        i = i + 1
 
# Shift the last line index to the end of list:
#   The last line index can be in the middle of the list.
last_lines = last_lines[(i%lines_size):] + last_lines[:(i%lines_size)]
 
# Display the last 5 lines.
#   Will display the 5th line from the end, 4th line from the end,
#       and so on.
for line in last_lines:
    print(line)

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.