Tailer - GNU tail/head in Python

0.4.1 · active · verified Thu Apr 16

Tailer is a Python library that provides simple implementations of the GNU `tail` and `head` utilities, allowing you to read the last few lines of a file or follow a file for new content as it's written. The current version is 0.4.1, and the project has a low release cadence, indicating stability for its core functionality.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `tailer.tail` to read the last few lines of a file and `tailer.head` to read the first few lines. It first creates a dummy file, then applies both functions, ensuring to pass an opened file object to them.

import tailer
import os

# Create a dummy log file for demonstration
file_path = 'my_log_file.log'
with open(file_path, 'w') as f:
    f.write('Line 1\n')
    f.write('Line 2\n')
    f.write('Line 3\n')
    f.write('Line 4\n')
    f.write('Line 5\n')
    f.write('Line 6\n')

# Read the last 3 lines using tailer.tail
print(f"Reading last 3 lines of '{file_path}':")
with open(file_path, 'r') as f:
    for line in tailer.tail(f, 3):
        print(line.strip())

# Read the first 2 lines using tailer.head
print(f"\nReading first 2 lines of '{file_path}':")
with open(file_path, 'r') as f:
    for line in tailer.head(f, 2):
        print(line.strip())

# Cleanup the dummy file
os.remove(file_path)

view raw JSON →