python - How to call a custom function along with the event when a file is modified? -


i need in being able make call my_custom_function when file being modified. want have state of file file_change, file_delete, etc available. i've used watchdog, supports predefined functions first parameters schedule(). should able make custom call , operations on it. use case file content before file modifications , after file modifications, using watchdog.

import sys import time import logging watchdog.observers import observer import os  def my_custom_function():     print "---"  if __name__ == "__main__":     logging.basicconfig(level=logging.info,                         format='%(asctime)s - %(message)s',                         datefmt='%y-%m-%d %h:%m:%s')     path = os.path.abspath(".")     my_event = my_custom_function()     observer = observer()     observer.schedule(my_event, path, recursive=true)     observer.start()     try:         while true:             time.sleep(1)     except keyboardinterrupt:         observer.stop()     observer.join() 

hope below code answer question.

import sys import time import watchdog watchdog.observers import observer watchdog.events import patternmatchingeventhandler import time threading import thread  class myhandler(patternmatchingeventhandler):      def process(self, event):         print "i being processed"      def on_modified(self, event):         print "file modified " + event.src_path         self.process(event)      def on_created(self, event):         print "file created" + event.src_path         self.process(event)      def on_moved(self, event):         print "file moved" + event.src_path         self.process(event)      def on_deleted(self, event):         print "file deleted" + event.src_path         self.process(event)  if __name__ == '__main__':     args = sys.argv[1:]       observer = observer()     observer.schedule(myhandler(), path=args[0] if args else '.')     print "start"     observer.start()      try:         while true:             time.sleep(1)     except keyboardinterrupt:         observer.stop()      observer.join() 

for more information http://pythonhosted.org/watchdog/api.html


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -