Python Forum
watchdog on_modified - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: watchdog on_modified (/thread-42021.html)



watchdog on_modified - CAD79 - Apr-23-2024

I'm creating a desktop application that notifies the user whenever a file has been modified, I've only just started using all of these modules and going through the documentation. Here is my code so far:

import time
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
from winotify import Notification, audio
import logging
import getpass


def on_modified(event):

    toast.set_audio(audio.Default, loop=False)
    toast.show()



if __name__ == '__main__':

    user = getpass.getuser()
    logging.basicConfig(filename='E:/DataLogging/log.log', filemode='a', level=logging.INFO,
                        format='%(asctime)s - %(message)s' + f' -  user: {user}',
                        datefmt='%Y-%m-%d %H:%M:%S')

    path = 'E:/Datalogging/'

    toast = Notification(app_id="Spreadsheet Notification",
                         title="Spreadsheet Updated",
                         msg=f'{user} has modified the spreadsheet',
                         icon=r'E:/download.jpg')

    event_handler = LoggingEventHandler()
    event_handler.on_modified = on_modified
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
        observer.join()
But, I've noticed that the user is notified any time someone opens up, modifies, and closes the file, but I only want the user to be notified whenever it is only modified, and not opened or closed.

Also, I cannot use the path 'E:/DataLogging/test.xlsx' but I can use 'E:/DataLogging/', which is the directory, and not the specific file. I'm sure there is an easy fix for this, but I cannot find the fix yet.

Could someone help me with these 2 issues?

Edit: I've also just been told that the real spreadsheet that will be observed is on a sharepoint google spreadsheet.

Can I use the watchdog observer for an online spreadsheet, or will I need to use a different module?


RE: watchdog on_modified - deanhystad - Apr-23-2024

Watchdog is not going to work with an online document. Online documents don't have a file system, so there is nothing to observe.

If this is a google sheet, or something similar, there is usually a way to set up notifications when the document is modified. This is provided by the document server and shouldn't require any programming.


RE: watchdog on_modified - CAD79 - Apr-24-2024

(Apr-23-2024, 03:09 PM)deanhystad Wrote: Watchdog is not going to work with an online document. Online documents don't have a file system, so there is nothing to observe.

If this is a google sheet, or something similar, there is usually a way to set up notifications when the document is modified. This is provided by the document server and shouldn't require any programming.

I wasn't told it was online at first, which was annoying, and all I had to do was select 'alert me' on their computer


RE: watchdog on_modified - deanhystad - Apr-24-2024

Quote:Also, I cannot use the path 'E:/DataLogging/test.xlsx' but I can use 'E:/DataLogging/', which is the directory, and not the specific file. I'm sure there is an easy fix for this, but I cannot find the fix yet.
watchdogs looks for changes to the file system, not to files. If you want to ignore modification events for other files in E:\Datalogging, you'll have to write that into your event callback.
def callback(event):
    """Print message if test.txt is modified."""
    if Path(event.src_path).name == "test.txt":
        print("File changed!!!")


event_handler = LoggingEventHandler()
event_handler.on_modified = callback
observer = Observer()
observer.schedule(event_handler, ".")
observer.start()