Python watchdog Watch For the Creation of New File By Pattern Match

守給你的承諾、 提交于 2021-01-29 12:10:57

问题


I'm still learning Python so I apologize up front. I am trying to write a watcher that watches for the creation of a file that matches a specific pattern. I am using watchdog with some code that is out there to get it watch a directory.

I don't quite understand how to have the watcher watch for a filename that matches a pattern. I've edited the regexes=[] field with what I think might work but I have not had luck.

Here is an example of a file that might come in: Hires For Monthly TM_06Jan_0946.CSV

I can get the watcher to tell me when a .CSV is created in the directory, I just can't get it to only tell me when essentially Hires.*\.zip is created.

I've checked the this link but have not had luck How to run the RegexMatchingEventHandler of Watchdog correctly?

Here is my code:

import time
import watchdog.events 
from watchdog.observers import Observer
from watchdog.events import RegexMatchingEventHandler
import re
import watchdog.observers 
import time
import fnmatch
import os

class Handler(watchdog.events.RegexMatchingEventHandler): 
    def __init__(self): 
        watchdog.events.RegexMatchingEventHandler.__init__(self, regexes=['.*'], ignore_regexes=[], ignore_directories=False, case_sensitive=False) 

    def on_created(self, event): 
        print("Watchdog received created event - % s." % event.src_path) 


    def on_modified(self, event): 
        print("Watchdog received modified event - % s." % event.src_path) 



if __name__ == "__main__": 
    src_path = r"C:\Users\Downloads"
    event_handler = Handler() 
    observer = watchdog.observers.Observer() 
    observer.schedule(event_handler, path=src_path, recursive=True) 
    observer.start() 
    try: 
        while True: 
            time.sleep(1) 
    except KeyboardInterrupt: 
        observer.stop() 
    observer.join() ```

回答1:


The reason why '.*' regex works and while the Hires.*\.zip regex doesn't is watchdog tries to match the full path with the regex. In terms of regex: r'Hires.*\.zip' will not give you a full match with "C:\Users\Downloads\Hires blah.zip"

Instead, try:

def __init__(self): 
    watchdog.events.RegexMatchingEventHandler.__init__(
            self, regexes=[r'^.*?\.cvs$', r'.*?Hires.*?\.zip'],
            ignore_regexes=[], ignore_directories=False, case_sensitive=False)

This will match with all the cvs files and the zip files starts with "Hires". The '.*?' matches with "C:\Users\Downloads" and the rest ensures file types, file names etc.



来源:https://stackoverflow.com/questions/60764150/python-watchdog-watch-for-the-creation-of-new-file-by-pattern-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!