j0h

inotify

j0h
Jan 8th, 2026
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from inotify.adapters import Inotify
  3. import os
  4. import time
  5.  
  6. WATCH = "/media/eri"
  7.  
  8. # Events that actually indicate a change
  9. INTERESTING = {
  10.     "IN_CREATE",
  11.     "IN_DELETE",
  12.     "IN_MOVED_TO",
  13.     "IN_MOVED_FROM",
  14.     "IN_CLOSE_WRITE",
  15.     "IN_ATTRIB",
  16. }
  17.  
  18. ino = Inotify()
  19. ino.add_watch(WATCH)
  20.  
  21. print(f"Watching {WATCH} (changes only)")
  22. print("Ctrl+C to stop\n")
  23.  
  24. try:
  25.     for event in ino.event_gen(yield_nones=False):
  26.         (_, type_names, path, filename) = event
  27.  
  28.         # Keep only meaningful changes
  29.         changes = [e for e in type_names if e in INTERESTING]
  30.         if not changes:
  31.             continue
  32.  
  33.         full = os.path.join(path, filename) if filename else path
  34.  
  35.         print(time.strftime("%H:%M:%S"), full)
  36.         print("  changes:", changes)
  37.         print()
  38.  
  39. except KeyboardInterrupt:
  40.     print("\nStopped.")
Advertisement
Add Comment
Please, Sign In to add comment