Created
September 22, 2022 02:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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