10

I need to monitor (using watchdog) a single file, not a whole directory.

What is the best way to avoid monitoring a whole directory? I suppose this

class watchdog.events.PatternMatchingEventHandler(patterns=None, ignore_patterns=None, ignore_directories=False, case_sensitive=False)[source]

could be helpful, but how to define an appropriate pattern for my file (C:/dir1/dir2/file.txt)?

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
Tom Cruise
  • 509
  • 1
  • 6
  • 10

2 Answers2

5

If you want to watch a file path like C:/dict1/dict2/file.txt, I think that's your pattern right there. There are no wildcards in, so it should be usable as-is.

As an aside, if Watchdog is giving you trouble, you could also consider Pyinotify: https://github.com/seb-m/pyinotify

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
2

The way to provide patterns for PatternMatchingEventHandler is

 from watchdog.events import PatternMatchingEventHandler

 class MyHandler(PatternMatchingEventHandler):
     patterns = ["*.xml", "*.log", "*/test.txt"] # */test.txt to watch that specifi file
ravi404
  • 7,119
  • 4
  • 31
  • 40
  • Python watchdog still watches the whole directory under the hood in Linux and generates inotify events internally, the handlers just filter off what you don't want to deal with. Watchdog is not the solution for this. Just activate debug logging for your project and you'll see. – René May 04 '19 at 12:01
  • hi @ravi404 and @Rene, what do i put in `ignore_patterns`, if i want to ignore every file format but ".csv" (my `patterns="*.csv"`)? – Naveen Reddy Marthala Nov 26 '19 at 10:40