You can subclass the watchdog.events.PatternMatchingEventHandler event handler and modify it to do whatever you want in the event of an event. You must set the template as the name of the file you want to control.
. pathtools.patterns, .
. , "" .
, , .
:
import sys, os.path, time, logging
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class MyEventHandler(PatternMatchingEventHandler):
def on_moved(self, event):
super(MyEventHandler, self).on_moved(event)
logging.info("File %s was just moved" % event.src_path)
def on_created(self, event):
super(MyEventHandler, self).on_created(event)
logging.info("File %s was just created" % event.src_path)
def on_deleted(self, event):
super(MyEventHandler, self).on_deleted(event)
logging.info("File %s was just deleted" % event.src_path)
def on_modified(self, event):
super(MyEventHandler, self).on_modified(event)
logging.info("File %s was just modified" % event.src_path)
def main(file_path=None):
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
watched_dir = os.path.split(file_path)[0]
print 'watched_dir = {watched_dir}'.format(watched_dir=watched_dir)
patterns = [file_path]
print 'patterns = {patterns}'.format(patterns=', '.join(patterns))
event_handler = MyEventHandler(patterns=patterns)
observer = Observer()
observer.schedule(event_handler, watched_dir, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
if len(sys.argv) > 1:
path = sys.argv[1]
main(file_path=path.strip())
else:
sys.exit(1)
watchdog python script ( ):
(stackoverflow)[root@joeyoung.io stackoverflow]
watched_dir = /usr/local/src/stackoverflow/watchdog
patterns = /usr/local/src/stackoverflow/watchdog/schedule.xml
schedule.xml :
[root@joeyoung.io watchdog]
schedule.xml:
(stackoverflow)[root@joeyoung.io stackoverflow]
watched_dir = /usr/local/src/stackoverflow/watchdog
patterns = /usr/local/src/stackoverflow/watchdog/schedule.xml
2015-08-31 19:30:31 - File /usr/local/src/stackoverflow/watchdog/schedule.xml was just modified