Something like tail -f in Python

Original story: [tail -f in Python] (http://qiita.com/sakamotomsh/items/f1cc47010a90e1e790fb)

[Like tail in Python] (http://blog.liris.org/2010/01/pythontail.html) I don't think it's that hard.

Recently I saw an article that watchdog is useful.

I tried using this for file monitoring. To make it OS independent [PollingObserver] It seems to use (http://pythonhosted.org//watchdog/api.html#module-watchdog.observers.polling).

tail-f.py


#!/usr/bin/env python
import os
import sys
import time
from os.path import dirname, exists

from watchdog.events import FileSystemEventHandler
from watchdog.observers.polling import PollingObserver


class TailHandler(FileSystemEventHandler):

    def __init__(self, path):
        self.path = path
        self.file = open(path, 'r')
        self.pos = os.stat(path)[6]

    def close(self):
        self.file.close()

    def print_line(self):
        self.file.seek(self.pos)
        for block in iter(lambda: self.file.read(32), ''):
            print(block, end='')
        self.pos = self.file.tell()

    def on_modified(self, event):
        if event.is_directory or self.path != event.src_path:
            return
        self.print_line()


def tail_like(path):
    observer = PollingObserver()
    handler = TailHandler(path)
    observer.schedule(handler, dirname(path))
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    finally:
        handler.close()
    observer.join()


def main():
    path = sys.argv[1]
    if not exists(path):
        print('{} is not found'.format(path))
        return 
    tail_like(path)


if __name__ == '__main__':
    main()

In each terminal do the following:

$ vm_stat -c 60 0.3 >> logs/vmstat.log
$ python tail-f.py logs/vmstat.log

Recommended Posts

Something like tail -f in Python
Something like JS setTimeout in python
Do something like Redis transactions in Python
I want to do something like sort uniq in Python
Display characters like AA in python
Do something like a Python interpreter in Visual Studio Code
Something like 40-32 / 2 = 4!
I wanted to do something like an Elixir pipe in Python
Find files like find on linux in Python
#I tried something like Vlookup with Python # 2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
StepAIC in Python
N-gram in python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Convert Unicode escape sequences like \ u in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python