Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a pythonic equivalent of tail -F?

Tags:

python

file

tail

What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?

In shell I might say:

tail -f "$file" | grep "$string" | while read hit; do     #stuff done 
like image 961
pra Avatar asked Nov 09 '09 20:11

pra


People also ask

How do you implement tail command in Python?

To my knowledge there's no equivalent to "tail" in the Python function list. Solution would be to use tell() (get file size) and read() to work out the ending lines.

What is the opposite of head () in Python?

The Tail is opposite to the head. It displays the ordered data from below.

How do I read the last 10 lines of a file in Python?

As we know, Python provides multiple in-built features and modules for handling files. Let's discuss different ways to read last N lines of a file using Python. In this approach, the idea is to use a negative iterator with the readlines() function to read all the lines requested by the user from the end of file.

What is tail command in Python?

The tail function in Python displays the last five rows of the dataframe by default. It takes in a single parameter: the number of rows. We can use this parameter to display the number of rows of our choice.


1 Answers

Well, the simplest way would be to constantly read from the file, check what's new and test for hits.

import time  def watch(fn, words):     fp = open(fn, 'r')     while True:         new = fp.readline()         # Once all lines are read this just returns ''         # until the file changes and a new line appears          if new:             for word in words:                 if word in new:                     yield (word, new)         else:             time.sleep(0.5)  fn = 'test.py' words = ['word'] for hit_word, hit_sentence in watch(fn, words):     print "Found %r in line: %r" % (hit_word, hit_sentence) 

This solution with readline works if you know your data will appear in lines.

If the data is some sort of stream you need a buffer, larger than the largest word you're looking for, and fill it first. It gets a bit more complicated that way...

like image 81
Jochen Ritzel Avatar answered Sep 20 '22 11:09

Jochen Ritzel