0

In this link, in the accepted answer, the Unix based solution is given. I want to do the same in windows. Since, i-node doesn't work with windows and python 2.7 stack I can't find any nice way to read a log file. My current way is something like this:

import time

cur = 0
while True:
    try:
        with open('myfile') as f:
            f.seek(0,2)
            if f.tell() < cur:
                f.seek(0,0)
            else:
                f.seek(cur,0)
            for line in f:
                print line.strip()
            cur = f.tell()
    except IOError, e:
        pass
    time.sleep(1)

The problem with this is the last lines will be missed if the lines are appended while time-sleeping, and rotation occurs before wake up. It is important for me to read all the lines. Is there any way I can do this in windows?

Pratik
  • 1,351
  • 1
  • 20
  • 37
  • 1
    Did you try this solution http://code.activestate.com/recipes/577968-log-watcher-tail-f-log/ from the post you referred to ? – May.D Jan 18 '19 at 12:32
  • If it really is "important", you need a better design that doesn't depend on a [kludge](https://en.wikipedia.org/wiki/Kludge) that misuses a state-based data store (the filesystem) as an event-based messaging channel. – Andrew Henle Jan 18 '19 at 12:39
  • Yeah, for windows the get_file_id function returns st_ctime which is not actually the id of the file. still, this solution works in all cases I can think of if there is some way we can get actual file Id I would be more happy :) – Pratik Jan 18 '19 at 12:39

0 Answers0