Python watchdog

想监控本地文件系统下的某个目录,原本是想用pyinotify的,谁知道mac的最新系统不支持,于是找了watchdog。根据官网的API做了一份和pyinotify的demo。

<pre>
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# author : firefoxbug
# E-Mail : wanghuafire@gmail.com
# Blog   : www.firefoxbug.net

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):
	def on_created(self,event):
		if event.is_directory:
			print event.event_type,event.src_path
		else :
			print event.event_type,event.src_path

	def on_deleted(self,event):
		if event.is_directory:
			print event.event_type,event.src_path
		else :
			print event.event_type,event.src_path

	def on_modified(self,event):
		if not event.is_directory:
			print event.event_type,event.src_path

	def on_moved(self,event):
		print "move",event.src_path,event.dest_path

if __name__ == "__main__":
	event_handler = MyHandler()
	observer = Observer()
	observer.schedule(event_handler, path='.', recursive=True)
	observer.start()

	try:
		print "started myWatch"
		while True:
			time.sleep(1)
	except KeyboardInterrupt:
		observer.stop()
	observer.join()
</pre>

详细的可以参考 http://pythonhosted.org/watchdog/

标签:none

评论已关闭