Reputation: 15750
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 am not sure where to add the code for when files change:
import time
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path='.', recursive=False)
observer.start()
try:
while True:
time.sleep(1)
else:
print "got it"
except KeyboardInterrupt:
observer.stop()
observer.join()
Where do I add the "got it" — in the while
loop if the files have been added/changed?
Upvotes: 69
Views: 103737
Reputation: 1313
I wanted just file modified to be observed - What I did was combining run_the_race's answer with an isinstance
check.
class FileChangeHandler(FileSystemEventHandler):
def __init__(self):
self.last_modified = datetime.now()
def on_modified(self, event):
if event.is_directory:
return
if not isinstance(event, FileModifiedEvent):
return
if datetime.now() - self.last_modified < timedelta(seconds=1):
return
self.last_modified = datetime.now()
do_what_you_want_here() # modify this line
Upvotes: 0
Reputation: 313
Here is the code similar to previous answers if you want to watch a specific file and get the content that was changed on it since last checking:
#!/usr/bin/python
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def __init__(self):
self.last_content = None
def on_modified(self, event):
if not event.is_directory and event.src_path.endswith('filename.txt'):
with open(event.src_path, "r") as f:
content = f.read()
if self.last_content is not None and content != self.last_content:
changed = content[len(self.last_content):]
print(changed)
self.last_content = content
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='.', recursive=False)
observer.start()
print('File monitor started')
try:
while True:
time.sleep(10) #sleep seconds
except KeyboardInterrupt:
observer.stop()
observer.join()
Upvotes: 0
Reputation: 93
For others that came across this question and want the answer for: "how to watch a specific file", also asked by @Cmag, this is a quick way to watch specific files. You set the specific files as regexes and use
directory_to_watch = './'
event_handler = RegexMatchingEventHandler(regexes=['.*/config.json'])
observer = Observer()
observer.schedule(event_handler, directory_to_watch, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
Upvotes: 0
Reputation: 1044
Instead of datetime, you may go with the src_path check logic since if the logic after the checking more than 1-second datetime logic will fail.
class EventHandler(FileSystemEventHandler):
def __init__(self):
self.src_path = ''
def on_modified(self, event):
if self.src_path == event.src_path:
return
else:
self.src_path = event.src_path
logger.info(f"{event.event_type} occured on file {self.src_path}")
#your long processing logics goes here.
Upvotes: 0
Reputation: 2318
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
Upvotes: 13
Reputation: 473763
Instead of LoggingEventHandler
define your handler:
#!/usr/bin/python
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print(f'event type: {event.event_type} path : {event.src_path}')
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='/data/', recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
on_modified
is called when a file or directory is modified.
Upvotes: 128