I want to create a module that will watch for folder. I write some of code:
import os, pyinotify
class FileWatcher:
def start_watch(self, dir):
wm = pyinotify.WatchManager()
self.notifier = pyinotify.Notifier(wm, EventProcessor())
mask = pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_DELETE | pyinotify.IN_DELETE_SELF | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO
wdd = wm.add_watch(dir, mask, rec=True)
while True:
self.notifier.process_events()
if self.notifier.check_events():
self.notifier.read_events()
def stop_watch(self):
self.notifier.stop()
print ('\nWatcher stopped')
class EventProcessor(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
print('in CREATE')
def process_IN_MODIFY(self, event):
print('in MODIFY')
def process_IN_DELETE(self, event):
print('in delete')
def process_IN_DELETE_SELF(self, event):
print('in delete self')
def process_IN_MOVED_FROM(self, event):
print('in MOVED_FROM')
def process_IN_MOVED_TO(self, event):
print('in IN_MOVED_TO')
if __name__ == "__main__":
watcher = FileWatcher()
try:
folder = "/home/user/Desktop/PythonFS"
watcher.start_watch(folder)
except KeyboardInterrupt:
watcher.stop_watch()
When i modified a file and then removed it the methods process_IN_MODIFY and process_IN_DELETE was never called. How cat i solve it?
But when i create a file the method process_IN_CREATE() was called.
OS is Linux mint 13.
UPD: New Code