17

每当目录中有任何更改(一个特定文件除外)时,我都会尝试使用看门狗运行同步脚本。我只是从自述文件(粘贴在下面)中复制了代码,这就是它所说的;记录哪个文件已更改。

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'

有人知道我在这里做错了什么吗?欢迎所有提示!

附言。我还想排除文件夹中的一个文件被监视。有什么想法我应该怎么做?

4

2 回答 2

29

您需要子类化并在调度中做任何您想做的事情:

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

class Event(LoggingEventHandler):
    def dispatch(self, event):
        print("Foobar")

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 = Event()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

如果您运行代码,您将Foobar在检测到更改时看到输出,要忽略文件,您可能需要使用 [events.PatternMatchingEventHandler][1]。每个都有不同的方法

要在某事被修改的情况下做某事,我们可以覆盖on_modified

class Event(LoggingEventHandler):
    def on_modified(self, event):
        print("Doh")

使用上面的类运行代码event_handler = Event() 并更改文件将输出如下内容:

Doh
Doh
Doh
Doh
Doh
Doh
Doh
2015-10-03 15:33:55 - Created file: ./test.txt___jb_bak___
2015-10-03 15:33:55 - Moved file: from ./test.txt to ./test.txt___jb_old___
2015-10-03 15:33:55 - Moved file: from ./test.txt___jb_bak___ to ./test.txt
2015-10-03 15:33:55 - Deleted file: ./test.txt___jb_old___
Doh

[1]: http: //pythonhosted.org/watchdog/api.html#watchdog.events.PatternMatchingEventHandler EventHandler类你可以覆盖,这完全取决于你想要做什么。LoggingEventHandler类 itslef 是 的子类watchdog.events.FileSystemEventHandler

类 watchdog.events.FileSystemEventHandler 基础:对象

Base file system event handler that you can override methods from.

dispatch(event) 将事件 分派给适当的方法。

Parameters: event (FileSystemEvent) – The event object representing the file system event.

on_any_event(event) 包罗万象 的事件处理程序。

Parameters: event (FileSystemEvent) – The event object representing the file system event.

on_created(event) 创建文件或目录时调用。

Parameters: event (DirCreatedEvent or FileCreatedEvent) – Event representing file/directory creation.

on_deleted(event) 删除文件或目录时调用。

Parameters: event (DirDeletedEvent or FileDeletedEvent) – Event representing file/directory deletion.

on_modified(event) 当文件或目录被修改时调用。

Parameters: event (DirModifiedEvent or FileModifiedEvent) – Event representing file/directory modification.

on_moved(event) 当文件或目录被移动或重命名时调用。

Parameters: event (DirMovedEvent or FileMovedEvent) – Event representing file/directory movement.
于 2015-10-03T14:17:49.037 回答
2

python中有很多方法可以跟踪目录中所做的更改。一种这样的方法是使用watchdog模块。

  • 需要的模块
    • 要安装看门狗,请在终端中运行此命令。
pip install watchdog

如果您想在"."创建或修改文件/目录时进行更改(当前位于根目录 - ),您可以使用以下代码来执行此操作:

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


class Watcher:
    def __init__(self, path):
        self.observer = Observer()
        self.path = path

    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.path, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(1)
        except:
            self.observer.stop()
            print("Error")

        self.observer.join()


class Handler(FileSystemEventHandler):
    @staticmethod
    def on_any_event(event):
        # if event.is_directory:
        #     return None
        print(
            "[{}] noticed: [{}] on: [{}] ".format(
                time.asctime(), event.event_type, event.src_path
            )
        )


if __name__ == "__main__":
    w = Watcher(".")
    w.run()
  • event.src_path将是完整的文件路径
  • event.event_type将被创建移动等。

如果您想忽略目录更改,只需删除评论。

输出:

[Tue Feb  9 00:16:02 2021] noticed: [created] on: [/Users/mt/Documents/stackoverflow/test.txt] 
[Tue Feb  9 00:16:02 2021] noticed: [modified] on: [/Users/mt/Documents/stackoverflow] 
[Tue Feb  9 00:16:19 2021] noticed: [created] on: [/Users/mt/Documents/stackoverflow/download.jpg] 
[Tue Feb  9 00:16:19 2021] noticed: [modified] on: [/Users/mt/Documents/stackoverflow] 
[Tue Feb  9 00:16:30 2021] noticed: [created] on: [/Users/mt/Documents/stackoverflow/new_folder] 
[Tue Feb  9 00:16:30 2021] noticed: [modified] on: [/Users/mt/Documents/stackoverflow] 
[Tue Feb  9 00:16:46 2021] noticed: [deleted] on: [/Users/mt/Documents/stackoverflow/new_folder] 
[Tue Feb  9 00:16:46 2021] noticed: [modified] on: [/Users/mt/Documents/stackoverflow] 
[Tue Feb  9 00:16:52 2021] noticed: [deleted] on: [/Users/mt/Documents/stackoverflow/download.jpg] 
[Tue Feb  9 00:16:52 2021] noticed: [modified] on: [/Users/mt/Documents/stackoverflow] 
[Tue Feb  9 00:17:00 2021] noticed: [deleted] on: [/Users/mt/Documents/stackoverflow/test.txt] 
[Tue Feb  9 00:17:00 2021] noticed: [modified] on: [/Users/mt/Documents/stackoverflow] 

Observer是监视任何文件系统更改然后将事件分派给事件处理程序的类。它监视文件系统并查找任何更改。

是一个对象,当EventHandler文件系统发生某些事情时将被通知。通常,编写脚本来监视创建或修改的任何类型的新文件,例如 csv、txt、xml、jpg 等。

例如,在下面的代码中,PatternMatchingEventHandler继承自FileSystemEventHandler该类并用于执行此操作。这个类的一些有用的方法是:

  • on_any_event: 将针对任何事件执行。
  • on_created: 创建文件或目录时执行。
  • on_modified: 当文件被修改或目录被重命名时执行。
  • on_deleted:删除文件或目录时执行。
  • on_moved:移动文件或目录时执行。

以下脚本用于仅使用.csv文件观察PatternMAtchingEventHandler. 如果您想观察不止一种类型的文件,您可以进一步扩展模式列表。

import watchdog.events
import watchdog.observers
import time
import sys


class Handler(watchdog.events.PatternMatchingEventHandler):
    def __init__(self):
        # Set the patterns for PatternMatchingEventHandler
        watchdog.events.PatternMatchingEventHandler.__init__(
            self,
            patterns=["*.csv"],
            ignore_directories=True,
            case_sensitive=False,
        )

    def on_any_event(self, event):
        print(
            "[{}] noticed: [{}] on: [{}] ".format(
                time.asctime(), event.event_type, event.src_path
            )
        )


if __name__ == "__main__":
    path = sys.argv[1] if len(sys.argv) > 1 else "."
    event_handler = Handler()
    observer = watchdog.observers.Observer()
    observer.schedule(event_handler, path=path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
  • 在这里,PatternMAtchingEventHandler我们可以利用处理与扩展名为.csv的文件相关的事件。

输出:

[Tue Feb  9 00:18:51 2021] noticed: [created] on: [/Users/mt/Documents/stackoverflow/test.csv] 
[Tue Feb  9 00:18:59 2021] noticed: [modified] on: [/Users/mt/Documents/stackoverflow/test.csv] 
[Tue Feb  9 00:19:12 2021] noticed: [deleted] on: [/Users/mt/Documents/stackoverflow/test.csv] 
于 2021-02-08T23:20:44.207 回答