In this article, we will discuss a memory-efficient solution to read the last line or last N lines from a text or CSV file in python. Then we will also see how to real only last line or check if the last line in the file matches the given line.

We have created a function to read last N lines from a text file,

This function accepts 2 arguments i.e. a file path as a string and an integer N ( number of lines to be read from last). It returns a list of last N lines of the file.

How does this function work?

First of all, it creates an empty list to store last N lines of a file. Then it opens the given file for reading in binary format and starts reading each byte from the end of the file until the start of the file i.e.in reverse direction. While reading bytes, as soon as it encounters a new line character ‘\n’, it means a line is read successfully. It then reverses the string and add that string / line in a list and continues reading next bytes from the file in reverse direction till the top of the file is reached or our list size becomes N.

It internally uses two functions i.e.

  • file_object.tell(): It gives the pointer’s current position in the file, i.e. number of bytes from the beginning of the file.
  • file_object.seek(offset, reference_point): It moves the pointer to a reference_point + offset

 Let’s use the above-created function to fetch last N lines from a text file,

Suppose we have a text file ‘sample.txt’ & its contents are,

Now we will fetch last N lines from this file,

Get Last 3 lines of a text file as a list in python

Output:

It returned the last 3 lines from file ‘sample.txt’ as a list of strings and then we iterated over the list to print the last 3 lines of the file.

Let’s look at another example,

Get last 5 lines of a text file or CSV file

Output:

Efficiency of solution:

This is an efficient solution because we read the lines from last only and at max only N lines were in memory at a time.

So, even if we have large file with size in GBs and we want to read last 10 lines, then this solution will give results in efficiently because we started from last and read till last 10 lines only, it doesn’t matter how large the file was.

Read the last line of a text file or CSV file or log file

we can use the same function, to read the last line of a file. We need to pass the 1 as argument N in function get_last_n_lines() and it will return a list containing the last line. For example,

Output:

This is how we read and printed the last line from a file. It is also an efficient solution even in case of large files because we started reading from last and in backward direction.

Check if the last line in the file matches the given line

Let’s check if the last line in file ‘sample.txt’ is exactly ‘This is the end of file’,

Output:

It proves that the last line of the file matches the given string.

Check if the last line in the file contains given substring

Let’s check if the last line of file ‘sample.txt’ contains the string ‘is’

Output:

It proves that the last line of the file includes the given substring.

The complete example is as follows,

 

Output:

O

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.