Skip to content

Instantly share code, notes, and snippets.

@zer0yu
Created September 22, 2022 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zer0yu/b80d8f21ef220cbaefa19adce1984cff to your computer and use it in GitHub Desktop.
Save zer0yu/b80d8f21ef220cbaefa19adce1984cff to your computer and use it in GitHub Desktop.
File monitoring: for monitoring whether a specified file has been accessed or modified
import pyinotify
filechange_history = []
class MyEventHandler(pyinotify.ProcessEvent):
def process_IN_ACCESS(self, event)::
filechange_history.append("ACCESS")
print("ACCESS EVENT: ", event.pathname)
def process_IN_ATTRIB(self, event):
filechange_history.append("ATTRIB")
print("ATTRIB CHANGE EVENT:", event.pathname)
def process_IN_CREATE(self, event):
filechange_history.append("CREATE")
print("CREATE EVENT:", event.pathname)
def process_IN_DELETE(self, event):
filechange_history.append("DELETE")
print("DELETE EVENT:", event.pathname)
def process_IN_MODIFY(self, event):
filechange_history.append("MODIFY")
print("MODIFY EVENT:", event.pathname)
def process_IN_OPEN(self, event):
filechange_history.append("OPEN")
print("OPEN EVENT:", event.pathname)
def file_observer():
monitor_obj = pyinotify.WatchManager()
monitor_obj.add_watch("/flag", pyinotify.ALL_EVENTS, rec=True)
# event handler
event_handler= MyEventHandler()
# notifier
monitor_loop= pyinotify.Notifier(monitor_obj, event_handler)
monitor_loop.loop()
if __name__ == '__main__':
file_observer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment