tail -f with python
pyth at devel.trillke.net
Sat Aug 3 14:57:29 EDT 2002
Roy Marteen wrote:
> How can I write in Python something like 'tail -f filename'? I mean, when
> there is an append to the file, it will be displayed real time.
> I tried this:
> while 1:
> line = open('test', 'r').readline()
> print line
> But it keeps looping, ...
as it should. it reads the first line of 'test' and prints it,
There are two possibilities. First is simply invoking 'tail -f'
in the background, basically:
tailoutputfile = os.popen('tail -f syslog')
line = tailoutputfile.readline()
if len(line)==0: # change the termination condition
second is doing what 'tail -f' does itself. Basically:
part = f.read(size-lastsize) # reads chunks, not lines!
time.sleep(0.2) # seconds to wait
print >>sys.stderr, "file filename got truncated"
The code is hopefully self-explaining enough.
If not, feel free to ask.
More information about the Python-list