Last active
June 15, 2017 14:55
-
-
Save finghine/9700b669724baf500fdc09615824d90e to your computer and use it in GitHub Desktop.
监视文件变化 watchdog
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
from watchdog.observers import Observer | |
from watchdog.events import * | |
import time | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
class FileEventHandler(FileSystemEventHandler): | |
def __init__(self): | |
FileSystemEventHandler.__init__(self) | |
def on_moved(self, event): | |
if event.is_directory: | |
print("directory moved from {0} to {1}".format(event.src_path,event.dest_path)) | |
else: | |
print("file moved from {0} to {1}".format(event.src_path,event.dest_path)) | |
def on_created(self, event): | |
if event.is_directory: | |
print("directory created:{0}".format(event.src_path)) | |
else: | |
print("file created:{0}".format(event.src_path)) | |
def on_deleted(self, event): | |
if event.is_directory: | |
print("directory deleted:{0}".format(event.src_path)) | |
else: | |
print("file deleted:{0}".format(event.src_path)) | |
def on_modified(self, event): | |
if event.is_directory: | |
print("directory modified:{0}".format(event.src_path)) | |
else: | |
print event.event_type | |
print("file modified:{0}".format(event.src_path)) | |
if __name__ == "__main__": | |
observer = Observer() | |
event_handler = FileEventHandler() | |
observer.schedule(event_handler,"C:/watchdog_test/example",True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment