pythonpython-3.xpython-watchdog

Monitoring a folder for files


Could you please tell me how can I implement the possibility of detecting two files with the same extension in a folder?

That is, I need a folder that will be tracked, and as soon as two files with the extension .txt appear in this folder, then it is necessary to identify the file created earlier than everyone else in the same folder (i.e. it will be the oldest) and display its name on the screen, for example, or any other functions.

I found this method that can track the creation of files in a folder... but unfortunately, the level of my knowledge of Python does not allow me to further develop the idea that I described above.

#!/usr/bin/env python3

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


class EventHandler(FileSystemEventHandler):
    def on_created(self, event):
        result = (event.event_type, event.src_path)
        print(result)


if __name__ == "__main__":

    path = r"/root/test/"
    event_handler = EventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

Solution

  • Based on your script:

    import time
    import os
    from watchdog.observers import Observer
    from watchdog.events import FileSystemEventHandler
    
    
    class EventHandler(FileSystemEventHandler):
    
        @staticmethod
        def on_created(event):
            action, path = (event.event_type, event.src_path)
            if not "." in path.split(os.sep)[-1]:
                return
            path = ".".join(path.split("."))
            extension = path.split(".")[-1]
            dirname = os.path.dirname(path)
    
            files = os.listdir(dirname)
            same_ext_files = [os.path.join(dirname, f) for f in files if f.endswith("." + extension)]
            creation_date = [os.path.getmtime(f) for f in same_ext_files]
    
            print(f"{action = }")
            print(f"{path = }")
            print(f"{extension = }")
            print(f"{dirname = }")
            print(f"{files = }")
            print(f"{same_ext_files = }")
            print(f"{creation_date = }")
    
    
    if __name__ == "__main__":
    
        path = os.path.join("/root", "test/")
        if not os.path.isdir(path):
            raise FileNotFoundError
        observer = Observer()
        observer.schedule(EventHandler(), path, recursive=True)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()
    

    enter image description here

    Creating file Fichier texte (2).txt:

    python test.py 
    action = 'created'
    path = '/home/vince/Bureau/stackoverflow/Fichier texte (2).txt'
    extension = 'txt'
    dirname = '/home/vince/Bureau/stackoverflow'
    files = ['Fichier texte (2).txt', 'Fichier texte (1).txt', 'Fichier texte.txt', 'test.py']
    same_ext_files = ['/home/vince/Bureau/stackoverflow/Fichier texte (2).txt', '/home/vince/Bureau/stackoverflow/Fichier texte (1).txt', '/home/vince/Bureau/stackoverflow/Fichier texte.txt']
    creation_date = [1638378453.7645514, 1638378430.8448474, 1638378409.4784563]
    

    You have now two lists: same_ext_files and creation_date to pipe to anything you want.

    code fixed to working