Tailing a log file?

Kenji Noguchi tokyo246 at gmail.com
Fri Jun 22 14:50:45 EDT 2007


something like this? unix tail command does more fancy stuff
like it waits for timeout, and check if the file is truncated
or depending on incoming data it sleeps seconds , etc etc.

#!/usr/bin/env python
import sys, select

while True:
    ins, outs, errs = select.select([sys.stdin],[],[])
    for i in ins:
        print i.readline()


2007/6/22, Evan Klitzke <evan at yelp.com>:
> On 6/22/07, Evan Klitzke <evan at yelp.com> wrote:
> > Everyone,
> >
> > I'm interested in writing a python program that reads from a log file
> > and then executes actions based on the lines. I effectively want to
> > write a loop that does something like this:
> >
> > while True:
> >     log_line = log_file.readline()
> >     do_something(log_line)
> >
> > Where the readline() method blocks until a new line appears in the
> > file, unlike the standard readline() method which returns an empty
> > string on EOF. Does anyone have any suggestions on how to do this?
> > Thanks in advance!
>
> I checked the source code for tail and they actually poll the file by
> using fstat and sleep to check for changes in the file size. This
> didn't seem right so I thought about it more and realized I ought to
> be using inotify. So I guess I answered my own question.
>
> --
> Evan Klitzke <evan at yelp.com>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list