How to streamingly read text file and display whenever updated text

Andreas Perstinger andipersti at gmail.com
Mon Oct 7 05:52:25 EDT 2013


On 07.10.2013 03:54, galeomaga at gmail.com wrote:
> https://docs.google.com/file/d/0B2D69u2pweEvelh1T25ra19oZEU/edit?usp=sharing
>

For the readers who don't bother clicking on the link above: It's a 
short video where the OP demonstrates how her/his usage of tail doesn't 
work.

> no matter call tail directly in python or using the script of tail
> all failed
> it seems it can not read next line

In your video you use gedit to write some file and "tail -f <file>" to 
follow it. But "tail -f" will follow the file descriptor. Usually, 
editors like gedit won't save your changes to the original file but 
create a new temporary file and rename it later to the original file 
name after deleting the original one. Thus tail will follow an already 
deleted file.
See also this blog post:
http://tech.shantanugoel.com/2009/12/23/continuous-monitor-tail-fails.html

For your example you will have to use "tail -F <file>" which will follow 
the file name.

Alternatively you could write a simple script to simulate a continously 
growing file like

import time
for i in range(1000):
     with open("test.txt", "a") as f:
         f.write(str(i) + '\n')
     time.sleep(1)

which should work with "tail -f".

Bye, Andreas



More information about the Python-list mailing list