来自两个不同目录的文件的Python看门狗

时间:2017-06-23 11:12:09

标签: python python-watchdog

我正在尝试使用Python的监视程序模块来监听文件系统更改。我想监视两个不同目录中的文件。对于单个文件监视,我使用PatternMatchingEventHandler中的watchdog.events。我想对多个目录使用相同的内容。

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

class EventHandler(PatternMatchingEventHandler):
    def on_modified(self, event):
        super(EventHandler, self).on_modified(event)
        print event

if __name__ == "__main__":
    dir_name = ["/home/user1/first", "/home/user1/second"]
    observer = Observer()
    patterns = ["/home/user1/first/first.log","/home/user1/second/second.log")]
    for i in xrange(len(dir_name)):
        event_handler = EventHandler(patterns = patterns[i])
        observer.schedule(event_handler, dir_name[i], recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(0.1)
    except KeyboardInterrupt:
        observer.stop()

在上面的代码中,我尝试做多个目录观察格式并为每个文件创建一个事件处理程序。它不适合我。这里有什么我想念的吗?这样做的方法是什么?

感谢。

4 个答案:

答案 0 :(得分:0)

这里有些错误

EventHandler(patterns = patterns[i])

arg patterns是一种列表类型,因此您可以像这样使用

patterns = [["/home/user1/first/first.log"], ["/home/user1/second/second.log"]]
EventHandler(patterns = patterns[i])

答案 1 :(得分:0)

尽管它不使用看门狗库,但这只是检查是否添加或删除了特定类型文件的简单方法 如果要检查哪些文件,可以使用任何变量将其附加并存储在数组中

    import os
    import fnmatch
    import threading
    import time
    
    initial_count = 0
    flag = 0
    files = []
    
    path = ["/home/kirti/workspace/pythonproject6/img", "/home/kirti/workspace/pythonproject6/copy"]
    
    
    def taskcount(path, flag, initial_count):
        while 1:
            time.sleep(3)
            new_count = len(fnmatch.filter(os.listdir(path), "*.jpg"))
    
            if new_count > initial_count:
    
                if flag != 0:
                    print("Added \nCount :", new_count, "=", path)
                else:
                    print(new_count)
            if new_count < initial_count:
                print("Removed \nCount :", new_count, "=", path)
    
            initial_count = new_count
            flag = 1
    
    
    for j in range(len(path)):
        t = threading.Thread(target=taskcount, args=(path[j], flag, initial_count))
    
        t.start() 

答案 2 :(得分:0)

我使用的是 python3、LINUX 操作系统

按照上面建议的小修改和 myside 的一些补充,现在可以使用了

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

class EventHandler(PatternMatchingEventHandler):
        def on_modified(self, event):
                super(EventHandler, self).on_modified(event)
                print(event)

if __name__ == "__main__":
        dir_name = ["/home/don/test1", "/home/don/test2"]
        observer = Observer()
        threads=[]
        patterns = [['*.log'],['*.ok']]
        for i in range(len(dir_name)):
                event_handler = EventHandler(patterns = patterns[i],ignore_directories=True,case_sensitive=False)
                observer.schedule(event_handler, dir_name[i], recursive=True)
                threads.append(observer)
        observer.start()

        try:
                while True:
                        time.sleep(0.1)
        except KeyboardInterrupt:
                observer.stop()
        observer.join()

答案 3 :(得分:-1)

与我相比,你在EOF时缺少observer.join()。试试吧。

修改

请尝试以下代码:

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

class EventHandler(PatternMatchingEventHandler):
    def on_modified(self, event):
        super(EventHandler, self).on_modified(event)
        print event

if __name__ == "__main__":
    observer = Observer()
    patterns = ["/home/user1/first/first.log","/home/user1/second/second.log"]
    for pattern in patterns:
        event_handler = EventHandler(patterns=pattern)
        observer.schedule(event_handler, dir_name[i], recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(0.1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()