Skip to content

Instantly share code, notes, and snippets.

@GuoJing
Last active August 29, 2015 13:56
Show Gist options
  • Save GuoJing/8994944 to your computer and use it in GitHub Desktop.
Save GuoJing/8994944 to your computer and use it in GitHub Desktop.
import os
import time
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
def sync_delete(event):
print 'delete %s' % event.src_path
path = event.src_path
# delete file from server
try:
if os.path.isdir(path):
os.rmdir(path)
else:
os.remove(path)
except Exception, e:
print e
print 'sleep'
time.sleep(5)
def sync_create(event):
print 'create %s' % event.src_path
# upload to server
def main(path_to_watch):
event_handler = LoggingEventHandler()
observer = Observer()
event_handler.on_deleted = sync_delete
event_handler.on_created = sync_create
observer.schedule(event_handler, path_to_watch, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
path_to_watch = '/Users/guojing/Desktop/lala'
print 'start watching %s ...' % path_to_watch
main(path_to_watch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment