Created
May 29, 2018 10:52
-
-
Save ChristianWitts/5f67b503f6de04cb25de20a992a51c71 to your computer and use it in GitHub Desktop.
super naive tail -f in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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