每当目录中有任何更改(一个特定文件除外)时,我都会尝试使用看门狗运行同步脚本。我只是从自述文件(粘贴在下面)中复制了代码,这就是它所说的;记录哪个文件已更改。
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
我现在想在发生任何变化时运行一个函数(将整个文件夹同步到远程机器)。所以我只是event_handler
用我自己的函数替换。但这给了我以下错误:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/Library/Python/2.7/site-packages/watchdog/observers/api.py", line 199, in run
self.dispatch_events(self.event_queue, self.timeout)
File "/Library/Python/2.7/site-packages/watchdog/observers/api.py", line 368, in dispatch_events
handler.dispatch(event)
AttributeError: 'function' object has no attribute 'dispatch'
有人知道我在这里做错了什么吗?欢迎所有提示!
附言。我还想排除文件夹中的一个文件被监视。有什么想法我应该怎么做?