Skip to content

Instantly share code, notes, and snippets.

@scionoftech
Created June 13, 2019 13:25
Show Gist options
  • Save scionoftech/932fbe52478c0938d139c0cc71374c61 to your computer and use it in GitHub Desktop.
Save scionoftech/932fbe52478c0938d139c0cc71374c61 to your computer and use it in GitHub Desktop.
this is a python script to mointor a folder for changes
import time
import os
import shutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class StartMonitor(FileSystemEventHandler):
def __init__(self, monitor_path, move_path):
self.monitor_path = monitor_path
self.move_path = move_path
def on_created(self, event):
"""" this will be called when new file/folder created"""
# print(f'event type: {event.event_type} path : {event.src_path}')
self.move_file(event.src_path)
def startMonitor(self):
"""" this will monitor folder"""
observer = Observer()
observer.schedule(self, path=self.monitor_path,
recursive=False)
observer.start()
print('Started monitoring folder....')
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
def move_file(self, path):
"""" this will move file/folder to destination folder """
filename = os.path.basename(path)
shutil.move(path, self.move_path + os.sep + filename)
print(filename, " file moved to ", self.move_path)
if __name__ == '__main__':
ss = StartMonitor(SOURCE_PATH, DESTINSTION_PATH)
ss.startMonitor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment