nithir revised this gist . Go to revision
1 file changed, 7 insertions, 7 deletions
libtail.py
| @@ -2,20 +2,20 @@ | |||
| 2 | 2 | ||
| 3 | 3 | from time import sleep | |
| 4 | 4 | ||
| 5 | - | def tail (filename, n=0, polling=0.01): | |
| 6 | - | num_lines = sum(1 for line in open(filename)) | |
| 7 | - | f = open(filename,'r') | |
| 5 | + | def tail (filename, printed_lines_count=0, polling_interval_duration=0.01): | |
| 6 | + | line_count = sum(1 for line in open(FILENAME)) | |
| 7 | + | f = open(FILENAME,'r') | |
| 8 | 8 | cpt = 0 | |
| 9 | - | while cpt < num_lines - n : | |
| 9 | + | while cpt < line_count - printed_lines_count : | |
| 10 | 10 | cpt += 1 | |
| 11 | 11 | next(f) | |
| 12 | 12 | ||
| 13 | 13 | try: | |
| 14 | 14 | while True: | |
| 15 | 15 | try: | |
| 16 | - | l = next(f) | |
| 17 | - | print(filename,":",l[:-1]) | |
| 16 | + | line_read = next(f) | |
| 17 | + | print(filename,":",line_read[:-1]) | |
| 18 | 18 | except: | |
| 19 | - | sleep(polling) | |
| 19 | + | sleep(polling_interval_duration) | |
| 20 | 20 | except: | |
| 21 | 21 | f.close() | |
nithir revised this gist . Go to revision
2 files changed, 31 insertions
libtail.py(file created)
| @@ -0,0 +1,21 @@ | |||
| 1 | + | #!/usr/bin/env python3 | |
| 2 | + | ||
| 3 | + | from time import sleep | |
| 4 | + | ||
| 5 | + | def tail (filename, n=0, polling=0.01): | |
| 6 | + | num_lines = sum(1 for line in open(filename)) | |
| 7 | + | f = open(filename,'r') | |
| 8 | + | cpt = 0 | |
| 9 | + | while cpt < num_lines - n : | |
| 10 | + | cpt += 1 | |
| 11 | + | next(f) | |
| 12 | + | ||
| 13 | + | try: | |
| 14 | + | while True: | |
| 15 | + | try: | |
| 16 | + | l = next(f) | |
| 17 | + | print(filename,":",l[:-1]) | |
| 18 | + | except: | |
| 19 | + | sleep(polling) | |
| 20 | + | except: | |
| 21 | + | f.close() | |
tail.py(file created)
| @@ -0,0 +1,10 @@ | |||
| 1 | + | #!/usr/bin/env python3 | |
| 2 | + | ||
| 3 | + | from libtail import tail | |
| 4 | + | from sys import argv | |
| 5 | + | ||
| 6 | + | if len(argv) != 2: | |
| 7 | + | print("""utilisation: | |
| 8 | + | %s FICHIER"""%argv[0]) | |
| 9 | + | exit(1) | |
| 10 | + | tail(argv[1]) | |