Skip to content

Latest commit

 

History

History
58 lines (43 loc) · 1.78 KB

File metadata and controls

58 lines (43 loc) · 1.78 KB

File organiser to watch a directory and monitor any changes to the dir. Any files added will be placed into categorical folders (pics, docs, binaries, scripts etc).

Fixes:

Fix the current while loop bug where the code only creates folder and files once the watchdog is terminated.

Here is the recommended fix according to chatgpt:

BEGIN

IMPORT necessary modules:
    - watchdog.observers.Observer
    - watchdog.events.FileSystemEventHandler
    - os, time, pathlib, logging

DEFINE a dictionary of file types and their target folders:
    - Example: {"documents": [".pdf", ".docx"], "pictures": [".jpg", ".png"]}

CONFIGURE logging to print and/or save events

DEFINE FileEventHandler class (inherits from FileSystemEventHandler):

    METHOD on_created(event):
        IF event is a directory:
            RETURN (ignore)
        
        EXTRACT the file extension from the event's path
        FOR each category and list of extensions in file_types:
            IF the extension matches:
                CREATE destination folder if it doesn't exist
                MOVE the file into the destination folder
                LOG the move
                BREAK (stop checking other categories)

DEFINE start_watching(directory):

    CREATE FileEventHandler instance
    CREATE Observer
    SCHEDULE the handler to watch the given directory
    START the observer
    LOG that watching has started

    LOOP:
        Sleep briefly (e.g., 1 second)
        (Keeps program alive to watch files)

    ON KeyboardInterrupt (Ctrl+C):
        STOP the observer
        LOG that watcher has stopped
    JOIN observer thread

IN MAIN:
    SET the folder to watch (e.g., current working directory)
    CALL start_watching with this folder

END

Use the fileWatcher to manage all the logig and infrastructure