views:

99

answers:

4

Hi, I'm trying to find a nice way to read a log file in real time using python. I'd like to process lines from a log file one at a time as it is written. Somehow I need to keep trying to read the file until it is created and then continue to process lines until I terminate the process. Is there an appropriate way to do this? Thanks.

+1  A: 

Maybe you could do a system call to

tail -f

using os.system()

Guillaume Lebourgeois
+1  A: 

Take a look at this PDF starting at page 38, ~slide I-77 and you'll find all the info you need. Of course the rest of the slides are amazing, too, but those specifically deal with your issue.

Wayne Werner
+1  A: 

You could try with something like this:

import time

while 1:
    where = file.tell()
    line = file.readline()
    if not line:
        time.sleep(1)
        file.seek(where)
    else:
        print line, # already has newline

Example was extracted from here.

Pablo Santa Cruz
This seems to be working but it won't allow me to create objects or write to a database at the same time in my django app. I don't see an obvious reason for this; is there a simple fix?
William
I don't know. You should post some code in a separate question to get answers to this one, I guess. I don't see any reason not to get database updated if you place that code inside this one...
Pablo Santa Cruz
Got this to work but I had to mess with the string a lot before I could get it to write to my database. Thanks.
William
A: 

Depending on the OS you can also look into Pyinotify, which is a Python module built on inotify. Here are a few more inotify wrappers:

http://code.activestate.com/recipes/576375-low-level-inotify-wrapper/

http://code.activestate.com/recipes/576377-high-level-inotify-wrapper/

Garett