Python Forum
How would I use Watchdog to get triggered when DVD is inserted? - 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: How would I use Watchdog to get triggered when DVD is inserted? (/thread-34619.html)

Pages: 1 2


How would I use Watchdog to get triggered when DVD is inserted? - Daring_T - Aug-14-2021

I am making a DVD ripping script and I am trying to use the Watchdog Lib to trigger my main function. The problem is, I'm getting this error:
Error:
PermissionError: [WinError 21] The device is not ready.
Is there a way for Watchdog to wait till it's loaded?

This is my test code if that helps anyone and if anyone has any questions feel free to ask.
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

event_handler = FileSystemEventHandler()

# calling test functions
event_handler.on_created = lambda : print("created")
event_handler.on_deleted = lambda : print("deleted")
event_handler.on_modified = lambda : print("modified")
event_handler.on_moved = lambda : print("moved")
	
path = "D://"
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
	print("Monitoring")
	while True:
		time.sleep(1)
except KeyboardInterrupt:
	observer.stop()
	print("Terminating")
observer.join()



RE: How would I use Watchdog to get triggered when DVD is inserted? - jefsummers - Aug-14-2021

Is that the complete error message? No line number or traceback?


RE: How would I use Watchdog to get triggered when DVD is inserted? - Daring_T - Aug-14-2021

This happens when a DVD is not inserted and I want Watchdog to wait until a DVD is inserted then run my main function.
This is the complete traceback error:
Error:
Traceback (most recent call last): File "C:/Users/Daren/Documents/01 PYTHON/DVD Auto Ripper/watcher.py", line 16, in <module> observer.start() File "C:\Users\Daren\Documents\01 PYTHON\DVD Auto Ripper\venv\lib\site-packages\watchdog\observers\api.py", line 256, in start emitter.start() File "C:\Users\Daren\Documents\01 PYTHON\DVD Auto Ripper\venv\lib\site-packages\watchdog\utils\__init__.py", line 93, in start self.on_thread_start() File "C:\Users\Daren\Documents\01 PYTHON\DVD Auto Ripper\venv\lib\site-packages\watchdog\observers\read_directory_changes.py", line 67, in on_thread_start self._handle = get_directory_handle(self.watch.path) File "C:\Users\Daren\Documents\01 PYTHON\DVD Auto Ripper\venv\lib\site-packages\watchdog\observers\winapi.py", line 316, in get_directory_handle return CreateFileW(path, FILE_LIST_DIRECTORY, WATCHDOG_FILE_SHARE_FLAGS, File "C:\Users\Daren\Documents\01 PYTHON\DVD Auto Ripper\venv\lib\site-packages\watchdog\observers\winapi.py", line 112, in _errcheck_handle raise ctypes.WinError() PermissionError: [WinError 21] The device is not ready.



RE: How would I use Watchdog to get triggered when DVD is inserted? - jefsummers - Aug-15-2021

Looks like the error starts in line 45 which is not supplied.


RE: How would I use Watchdog to get triggered when DVD is inserted? - Daring_T - Aug-15-2021

(Aug-15-2021, 12:27 PM)jefsummers Wrote: Looks like the error starts in line 45 which is not supplied.
I just fixed my traceback error. The traceback should now line up with the test script.


RE: How would I use Watchdog to get triggered when DVD is inserted? - jefsummers - Aug-15-2021

Have you tried using a try..except block around line 15 (observer.start())?


RE: How would I use Watchdog to get triggered when DVD is inserted? - Daring_T - Aug-15-2021

I have tried using a try except block but still get the same error. I have even tried putting a 10 sec sleep time but it don’t help.


RE: How would I use Watchdog to get triggered when DVD is inserted? - jefsummers - Aug-16-2021

This is how I would do the try except
failed = True
while failed:
    try:
        observer.start()
        failed = False
    except:
        wait(5)
Is that what you tried? Should not have seen that error message, I would think


RE: How would I use Watchdog to get triggered when DVD is inserted? - deanhystad - Aug-16-2021

Is the DVD going to show up as D: or does it show up in D:? If it shows up as D: I don't think you can use watchdog to see when a DVD is installed. Sure, you can keep trying to start the observer and it will fail until a DVD is installed, but you get the same info trying to read the D:\\ folder.

In a windows program you would use the RegisterDeviceNotification call. I don't know of any python wrapper library for this system call.


RE: How would I use Watchdog to get triggered when DVD is inserted? - Daring_T - Aug-17-2021

(Aug-16-2021, 07:24 PM)jefsummers Wrote: This is how I would do the try except
failed = True
while failed:
    try:
        observer.start()
        failed = False
    except:
        wait(5)
Is that what you tried? Should not have seen that error message, I would think

It does work the way you wrote it and I don't what I messed up, but the thing is, I could have used the os library, while loop, and the time.sleep function to do the same thing. I wanted to use the Watchdog lib. to be more efficient with my script, but it looks like I am going beyond what this library can do.