Skip to content

Instantly share code, notes, and snippets.

@ChristianWitts
Created May 29, 2018 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristianWitts/5f67b503f6de04cb25de20a992a51c71 to your computer and use it in GitHub Desktop.
Save ChristianWitts/5f67b503f6de04cb25de20a992a51c71 to your computer and use it in GitHub Desktop.
super naive tail -f in python
#!/usr/bin/env python3
import sys
from datetime import datetime
import time
import os
def get_timestamp():
return datetime.now().strftime("[%Y%m%d %H:%M:%S]")
def tail_f(file):
interval = 0.01
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(interval)
file.seek(where)
else:
print("{} {}".format(get_timestamp(), line).strip(), end=None)
time.sleep(interval)
if __name__ == '__main__':
tail_f(open(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment