libtail.py
· 377 B · Python
Raw
#!/usr/bin/env python3
from time import sleep
def tail (filename, n=0, polling=0.01):
num_lines = sum(1 for line in open(filename))
f = open(filename,'r')
cpt = 0
while cpt < num_lines - n :
cpt += 1
next(f)
try:
while True:
try:
l = next(f)
print(filename,":",l[:-1])
except:
sleep(polling)
except:
f.close()
| 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() |
| 22 |
| 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]) |
| 11 |