blocking file.readlines() needed

U

Uwe Mayer

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
 
C

CptPicard

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()
 
P

Peter Otten

Uwe said:
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?

The naive way to do it:

while True:
while True:
line = f.readline()
if not line:
break
print line,
time.sleep(1)

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top