SHARE
TWEET

Inotify Python

DeaD_EyE Mar 21st, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pathlib import Path
  2. from inotify.adapters import Inotify
  3. from inotify.constants import IN_CLOSE_WRITE
  4.  
  5.  
  6. def watch(paths):
  7.     if isinstance(paths, (str, bytes, Path)):
  8.         paths = [paths]
  9.     notify = Inotify()
  10.     for path in paths:
  11.         notify.add_watch(path, mask=IN_CLOSE_WRITE)
  12.     events = notify.event_gen(yield_nones=False)
  13.     for _, event_types, path, file in events:
  14.         target = Path(path, file)
  15.         # print(target, event_types)
  16.         yield target
  17.  
  18.  
  19. notifier = watch('/home/andre')
  20. for file in notifier:
  21.     print(file.name, 'CONTENT:', file.read_text().strip())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top