watchdog monitoring file for changes

前端 未结 2 552
北海茫月
北海茫月 2020-12-07 09:38

I have a need to watch a log file for changes. After looking through stackoverflow questions, I see people recommending watchdog. So I\'m trying to test, and

2条回答
  •  天命终不由人
    2020-12-07 09:51

    Here's a snippet to prevent it running twice as others have commented in @alecxe answer:

    from datetime import datetime, timedelta
    
    class MyHandler(FileSystemEventHandler):
        def __init__(self):
            self.last_modified = datetime.now()
    
        def on_modified(self, event):
            if datetime.now() - self.last_modified < timedelta(seconds=1):
                return
            else:
                self.last_modified = datetime.now()
            print(f'Event type: {event.event_type}  path : {event.src_path}')
            print(event.is_directory) # This attribute is also available
    

提交回复
热议问题