Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to run function when file is modified python

import sys
import time
import watchdog
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
import time
from threading import Thread

class MyHandler(PatternMatchingEventHandler):

    def process(self, event):
        print("I am 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()
Comment

PREVIOUS NEXT
Code Example
Python :: pygame is not defined 
Python :: return Response converting to string drf 
Python :: get all non numeric columns pandas 
Python :: example of python application from github to docker image 
Python :: generate jwt token just passing userid in rest_framework_simplejwt 
Python :: how to simulate a keypress using pyautogui 
Python :: python autoLibrary 
Python :: python anywhere just says hello from flask 
Python :: group by month and year 
Python :: python abbreviated for loop 
Python :: # sort the dictionary 
Python :: pairplot hide original legend 
Python :: pairplot yaxis diagonal 
Python :: give the factorials of 6 in python 
Python :: how to make pictures whit python 
Python :: modern ui python 
Python :: Simple Python Permutation Printing result without for loop 
Python :: Python 2 vs Python 3 Print Statement 
Python :: stop level of the broker 
Python :: python count files fast 
Python :: python sqlite select where 
Python :: Elasticsearch scroll with Parallelism r 
Python :: Python NumPy atleast_3d Function Syntax 
Python :: df create dummy from multiple category 
Python :: Python NumPy asfortranarray Function Scalar to an array 
Python :: percentile of a score python 
Python :: if statment with logical or operator for multiple varaibles 
Python :: NumPy rot90 Example Rotating Twice 
Python :: NumPy invert Code When the input is a number 
Python :: numpy image processing 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =