python - Using pyinotify to 'live' refresh displayed file -
g'day,
i've got raspberry pi, used display transaction log csv file on hdmi-connected display. display operate live 'scoreboard', such user can see log csv file (like airport/flight announcing board).
i've been told pyinotify can monitor log csv file, , refresh file, without having close , reopen it? i've read through documentation, , searched web functionality, i've far come empty. don't have example code demonstrate i've tried (yet!), wanted ascertain first of whether functionality possible pyinotify, or whether should looking @ else.
i'm using python 3.3.
any guidance here amazing!
thanks!
ok, don't know if it's going here how can it:
let's have file :
echo "line 1" >> testfile.txt
than write script (make sure point file):
import os, pyinotify path = os.path.join(os.path.expanduser('~/'), 'testfile.txt') class eventhandler(pyinotify.processevent): def __init__(self, *args, **kwargs): super(eventhandler, self).__init__(*args, **kwargs) self.file = open(path) self.position = 0 self.print_lines() def process_in_modify(self, event): self.print_lines() def print_lines(self): new_lines = self.file.read() last_n = new_lines.rfind('\n') if last_n >= 0: self.position += last_n + 1 print new_lines[:last_n] else: print 'no line' self.file.seek(self.position) wm = pyinotify.watchmanager() handler = eventhandler() notifier = pyinotify.notifier(wm, handler) wm.add_watch(path, pyinotify.in_modify, rec=true) notifier.loop()
run file:
python notify.py
you see
line 1
than add line different terminal file (make sure script still running)
echo "line 2" >> testfile.txt
and see on script output
Comments
Post a Comment