How can I tail a log file in Python?

How can I tail a log file in Python?

Non Blocking

If you are on linux (as windows does not support calling select on files) you can use the subprocess module along with the select module.

import time
import subprocess
import select

f = subprocess.Popen([tail,-F,filename],
p = select.poll()

while True:
    if p.poll(1):
        print f.stdout.readline()

This polls the output pipe for new data and prints it when it is available. Normally the time.sleep(1) and print f.stdout.readline() would be replaced with useful code.


You can use the subprocess module without the extra select module calls.

import subprocess
f = subprocess.Popen([tail,-F,filename],
while True:
    line = f.stdout.readline()
    print line

This will also print new lines as they are added, but it will block until the tail program is closed, probably with f.kill().

Using the sh module (pip install sh):

from sh import tail
# runs forever
for line in tail(-f, /var/log/some_log_file.log, _iter=True):


Since sh.tail with _iter=True is a generator, you can:

import sh
tail = sh.tail(-f, /var/log/some_log_file.log, _iter=True)

Then you can getNewData with:

new_data =

Note that if the tail buffer is empty, it will block until there is more data (from your question it is not clear what you want to do in this case).


This works if you replace -f with -F, but in Python it would be locking. Id be more interested in having a function I could call to get new data when I want it, if thats possible. – Eli

A container generator placing the tail call inside a while True loop and catching eventual I/O exceptions will have almost the same effect of -F.

def tail_F(some_file):
    while True:
            for line in sh.tail(-f, some_file, _iter=True):
                yield line
        except sh.ErrorReturnCode_1:
            yield None

If the file becomes inaccessible, the generator will return None. However it still blocks until there is new data if the file is accessible. It remains unclear for me what you want to do in this case.

Raymond Hettinger approach seems pretty good:

def tail_F(some_file):
    first_call = True
    while True:
            with open(some_file) as input:
                if first_call:
          , 2)
                    first_call = False
                latest_data =
                while True:
                    if n not in latest_data:
                        latest_data +=
                        if n not in latest_data:
                            if not os.path.isfile(some_file):
                    latest_lines = latest_data.split(n)
                    if latest_data[-1] != n:
                        latest_data = latest_lines[-1]
                        latest_data =
                    for line in latest_lines[:-1]:
                        yield line + n
        except IOError:

This generator will return if the file becomes inaccessible or if there is no new data.


The second to last answer circles around to the top of the file it seems whenever it runs out of data. – Eli

I think the second will output the last ten lines whenever the tail process ends, which with -f is whenever there is an I/O error. The tail --follow --retry behavior is not far from this for most cases I can think of in unix-like environments.

Perhaps if you update your question to explain what is your real goal (the reason why you want to mimic tail –retry), you will get a better answer.

The last answer does not actually follow the tail and merely reads whats available at run time. – Eli

Of course, tail will display the last 10 lines by default… You can position the file pointer at the end of the file using, I will left a proper implementation as an exercise to the reader.

IMHO the approach is far more elegant than a subprocess based solution.

How can I tail a log file in Python?

The only portable way to tail -f a file appears to be, in fact, to read from it and retry (after a sleep) if the read returns 0. The tail utilities on various platforms use platform-specific tricks (e.g. kqueue on BSD) to efficiently tail a file forever without needing sleep.

Therefore, implementing a good tail -f purely in Python is probably not a good idea, since you would have to use the least-common-denominator implementation (without resorting to platform-specific hacks). Using a simple subprocess to open tail -f and iterating through the lines in a separate thread, you can easily implement a non-blocking tail operation in Python.

Example implementation:

import threading, Queue, subprocess
tailq = Queue.Queue(maxsize=10) # buffer at most 100 lines

def tail_forever(fn):
    p = subprocess.Popen([tail, -f, fn], stdout=subprocess.PIPE)
    while 1:
        line = p.stdout.readline()
        if not line:

threading.Thread(target=tail_forever, args=(fn,)).start()

print tailq.get() # blocks
print tailq.get_nowait() # throws Queue.Empty if there are no lines to read

Leave a Reply

Your email address will not be published. Required fields are marked *