Imitating "tail -f"

Ivan Voras ivoras at gmail.com
Sun Nov 22 03:43:31 CET 2009


I'm trying to simply imitate what "tail -f" does, i.e. read a file, wait
until it's appended to and process the new data, but apparently I'm
missing something.

The code is:

 54     f = file(filename, "r", 1)
 55     f.seek(-1000, os.SEEK_END)
 56     ff = fcntl.fcntl(f.fileno(), fcntl.F_GETFL)
 57     fcntl.fcntl(f.fileno(), fcntl.F_SETFL, ff | os.O_NONBLOCK)
 58
 59     pe = select.poll()
 60     pe.register(f)
 61     while True:
 62         print repr(f.read())
 63         print pe.poll(1000)

The problem is: poll() always returns that the fd is ready (without
waiting), but read() always returns an empty string. Actually, it
doesn't matter if I turn O_NDELAY on or off. select() does the same.

Any advice?



More information about the Python-list mailing list