Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

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

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:UsersDownloads"
    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() ```
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:UsersDownloadsHires 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:UsersDownloads" and the rest ensures file types, file names etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...