linux has a handy command called tail -f. I learned today that there is something called os.stat, so I wrote a little source. I am trying to monitor the file passed from the argument. It is realized only by moving to the end of the file, getting the current position of the file pointer, and reading the data from the file.
#-*- coding: utf-8 -*- import time import os import sys from stat import * def usage(): print "Usage: # python %s filename" % argv quit() def init(filename): file = open(filename,'r') #Move to the end of the file st_results = os.stat(filename) st_size = st_results[ST_SIZE] file.seek(st_size) return file def tail_f(file, usec): msec = usec / 1000 while 1: fpos = file.tell() line = file.readline() if not line: time.sleep(msec) file.seek(fpos) else: print line, #Not reached file.close() pass if __name__ == '__main__': argv = sys.argv argc = len(argv) if( argc != 2 ): usage() filename = argv file = init(filename) tail_f(file, 500)
By the way, if you only use it on linux, you can make it a wrapper script for tail -f.
import sys import os if __name__ == '__main__': argv = sys.argv argc = len(argv) if argc != 2: print "test", quit() filename = argv os.system('tail -f ' + filename )