Last active
August 29, 2015 13:56
-
-
Save GuoJing/8994944 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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