I have a tail -f like code snippet that I found somehwere on the web. Unfortunatelly, I found out at some point, that it seems to be stuck on EOF even when the EOF is not any longer there, because something was appended to the file. It will still return 0. If I seek back, for example 10 bytes, it will read up to the previous EOF position and that's all. I can fix it by closing and reopening the file, but I don't understand the behaviour. Can someone help?
The code:
def tail_f_nonblock(f):
while True:
where = f.tell()
line = f.readline()
if not line:
diff = f.tell()-where
f.seek(where)
# If there was some output, give -1
if diff!=0: return -1
else: return 0
else:
return line