0

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
marmeladze
  • 6,468
  • 3
  • 24
  • 45

1 Answers1

0

I'm assuming you're on a linux machine or similar. Did the inode number (use ls -i filename) change when the file was modified? If yes - essentially your old file was deleted, but it's contents are still available through the file handle your program is using (i.e. it still points to old inode). In such case reopening is the only possibility.

For more details see What happens to an open file handler on Linux if the pointed file gets moved, delete

Community
  • 1
  • 1
tmf
  • 58
  • 5