fork download
  1. #!/usr/bin/python
  2. import time
  3. from optparse import OptionParser
  4.  
  5. SLEEP_INTERVAL = 1.0
  6. i=0
  7. def readlines_then_tail(fin):
  8. print "Iterate through lines and then tail for further lines."
  9. i=0
  10. while True:
  11. line = fin.readline()
  12. if line:
  13. i=i+1
  14. print "line = ",i,line
  15. yield line
  16. else:
  17. tail(fin)
  18.  
  19. def tail(fin):
  20. print "Listen for new lines added to file."
  21. while True:
  22. where = fin.tell()
  23. print "where = ",where
  24. line = fin.readline()
  25. if not line:
  26. print "Going to sleep"
  27. print "zzz.zzz.zzz.zzz"
  28. time.sleep(SLEEP_INTERVAL)
  29. print "Jusi woke up !"
  30. fin.seek(where)
  31. else:
  32. yield line
  33.  
  34. def main():
  35. p = OptionParser("usage: tail.py file")
  36. (options, args) = p.parse_args()
  37. if len(args) < 1:
  38. p.error("must specify a file to watch")
  39. with open(args[0], 'r') as fin:
  40. for line in readlines_then_tail(fin):
  41. print line.strip()
  42.  
  43. if __name__ == '__main__':
  44. main()
  45.  
Runtime error #stdin #stdout #stderr 0.01s 24888KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Usage: tail.py file

prog: error: must specify a file to watch