blocking file.readlines() needed

CptPicard jpmorichon at earthlink.net
Fri Aug 13 19:16:29 CEST 2004


Geoff Howland has written a script, LogReader.py, that should give you some
hints on how to do your "tail -f".
http://unixnaut.com/skills/Languages/python/

Using google, I also found some talks about that. Here is one of the
solution that I found :


import sys,os
import time
from Tkinter import *
from ScrolledText import ScrolledText

class LogViewer(Frame):
    def __init__(self, parent, filename):
        Frame.__init__(self,parent)
        self.filename = filename
        self.file = open(filename, 'r')
        self.text = ScrolledText(parent)
        self.text.pack(fill=BOTH)
        data = self.file.read()
        self.size = len(data)
        self.text.insert(END, data)
        self.after(100, self.poll)

    def poll(self):
        if os.path.getsize(self.filename) > self.size:
            data = self.file.read()
            self.size = self.size + len(data)
            self.text.insert(END, data)
        self.after(100,self.poll)

if __name__ == "__main__":
    root = Tk()
    viewer = LogViewer(root, sys.argv[1])
    viewer.mainloop()

"Uwe Mayer" <merkosh at hadiko.de> wrote in message
news:cfiqlr$3a8$1 at news2.rz.uni-karlsruhe.de...
> Hi,
>
> I am looking for a way to make the call to a file objects readline()
method
> blocking when there is no more data, until data is appended to the file,
> similar to the way
>
> $ tail -F <system log file>
>
> works.
>
> However, file.readlines() aborts and returns an empty list.
>
> Any ideas?
>
> Thanks
> Ciao
> Uwe
>





More information about the Python-list mailing list