0

这个链接中,在接受的答案中,给出了基于 Unix 的解决方案。我想在 Windows 中做同样的事情。因为,i-node 不适用于 windows 和 python 2.7 堆栈,所以我找不到任何读取日志文件的好方法。我目前的方式是这样的:

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)

这样做的问题是,如果在时间睡眠时附加了行,则会错过最后一行,并且在唤醒之前发生旋转。阅读所有行对我来说很重要。有什么办法可以在 Windows 中做到这一点?

4

0 回答 0