Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
watchdog on_modified
#1
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?
Reply
#2
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.
Reply
#3
(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
Reply
#4
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error when using Watchdog and Qt to keep open a Queue before and after sending first pyhill00 0 1,616 Oct-28-2021, 09:10 AM
Last Post: pyhill00
  How would I use Watchdog to get triggered when DVD is inserted? Daring_T 12 4,838 Aug-17-2021, 01:49 PM
Last Post: Daring_T

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020