Implement tail command in Python

algorithms python

Introduction

In this article, we will write a simple implementation of the tail command from Linux. This program will take a file and an integer n as input and print the last n lines from the file. Also, the goal of the program is to not read the entire file into memory at once. This will make the program memory efficient when dealing with very large files.

Implementation Details

In order to implement this program, we will use queue data structure. We will read the file line by line and push each line into the queue. Every iteration, we will check the size of the queue. When the size of the queue is greater than or equal to n, we will remove the element that was first inserted in the queue. Since queue is a FIFO data structure, the element that was first inserted into the queue will be the first element that will be removed from the queue. This will ensure that the queue contains only last n lines from the file.

As a bonus, I have used argparse to handle the input from cli. Let’s look at the implementation of this program.

import argparse
import queue


def tail(filename, n):
    q = queue.Queue()
    size = 0

    with open(filename) as fh:
        for line in fh:
            q.put(line.strip())
            if size >= n:
                q.get()
            else:
                size += 1      

    for i in range(size):
        print(q.get())

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Print last n lines from a file.')
    parser.add_argument('file', type=str, help='File to read from')
    parser.add_argument('-n', type=int, default=10, help='The last n lines to be printed')
    args = parser.parse_args()

    tail(args.file, args.n)

Get new posts by email

Comments

comments powered by Disqus