如何在看门狗中输入文件名作为模式

时间:2019-02-20 21:53:09

标签: python watchdog python-watchdog

假设我有这样的代码

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

class MyHandler(PatternMatchingEventHandler):
    patterns = ["*/file.txt"]

    def process(self, event):
        """
        event.event_type 
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        # the file will be processed there
        print event.src_path, event.event_type  # print now only for degug
        if event.event_type == 'modified':
            print fname


    def on_modified(self, event):
        self.process(event)

if __name__ == '__main__':
#    args = sys.argv[1:]
    fname = 'file.txt'
    observer = Observer()
    observer.schedule(MyHandler(), path='.')
    observer.start()

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

    observer.join()

我的目标是让用户在MyHandler类的patterns变量中输入fname。

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

class MyHandler(PatternMatchingEventHandler):
    patterns = ["*/"+fname]

    def process(self, event):
        """
        event.event_type 
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        # the file will be processed there
        print event.src_path, event.event_type  # print now only for degug
        if event.event_type == 'modified':
            print fname


    def on_modified(self, event):
        self.process(event)

if __name__ == '__main__':
#    args = sys.argv[1:]
    fname = 'file.txt'
    observer = Observer()
    observer.schedule(MyHandler(), path='.')
    observer.start()

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

    observer.join()

我尝试了上述操作,但没有成功。它说fname is not defined。好吧,我尝试patterns = [].append("*/"+fname)也没有成功。

然后我尝试创建一个初始化函数

def __init__(fname):
    self.patterns = [].append("*/"+fname)

并通过了observer.schedule(MyHandler(fname), path='.')

笨蛋!也没有成功。我该如何实现?

1 个答案:

答案 0 :(得分:0)

您可以使用fnmatch.fnmatch将文件路径与shell模式进行匹配:

In [72]: import fnmatch
In [77]: fnmatch.fnmatch('/root/path/foo', '*/foo')
Out[77]: True

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

class MyHandler(PatternMatchingEventHandler):

    def init(self, patterns):
        self.patterns = ["*/"+pat for pat in patterns]

    def process(self, event):
        """
        event.event_type 
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        path = event.src_path
        print event.src_path, event.event_type  # print now only for degug        
        if any(fnmatch.fnmatch(path, pat) for pat in self.patterns):
            # the file will be processed there
            if event.event_type == 'modified':
                print(fname)

    def on_modified(self, event):
        self.process(event)

if __name__ == '__main__':
    args = sys.argv[1:]
    observer = Observer()
    observer.schedule(MyHandler(args), path='.')
    observer.start()

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

    observer.join()