Python pyinotify
# coding=utf-8
import os
import sys
import pyinotify
class Monitor(object):
def __init__(self, path):
self.path = path
def run(self):
handler = EventHandler()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, handler)
wm.add_watch(self.path, pyinotify.IN_MODIFY) # type: ignore
notifier.loop()
class EventHandler(pyinotify.ProcessEvent):
def process_IN_MODIFY(self, event):
print("File '{}' has been modified.".format(event.pathname))
def test_monitor():
path = '/home/user/demo.txt'
monitor = Monitor(path)
pid = os.getpid()
print("Test running with PID: {}".format(pid))
print("Monitoring file: {}".format(path))
monitor.run()
if __name__ == '__main__':
try:
path = '/home/user/demo.txt'
if not os.path.exists(path):
raise Exception("Path '{}' does not exist.".format(path))
monitor = Monitor(path)
monitor.run()
### Add testing code
# print("Starting testing...")
# test_monitor()
except Exception as e:
print(str(e))
sys.exit(1)
Records 2023-03-20 02:09:13 通过 网页 浏览(152)
共有0条评论!