Python Forum
How would I use Watchdog to get triggered when DVD is inserted?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How would I use Watchdog to get triggered when DVD is inserted?
#1
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()
Reply
#2
Is that the complete error message? No line number or traceback?
Reply
#3
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.
Reply
#4
Looks like the error starts in line 45 which is not supplied.
Reply
#5
(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.
Reply
#6
Have you tried using a try..except block around line 15 (observer.start())?
Reply
#7
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.
Reply
#8
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
Daring_T likes this post
Reply
#9
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.
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  watchdog on_modified CAD79 1 97 11 hours ago
Last Post: deanhystad
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 1,522 Sep-03-2022, 11:21 PM
Last Post: Winfried
  Error when using Watchdog and Qt to keep open a Queue before and after sending first pyhill00 0 1,594 Oct-28-2021, 09:10 AM
Last Post: pyhill00
  Halting if command if gpio triggered event triggered knoxvilles_joker 7 3,235 Jun-21-2021, 01:27 AM
Last Post: knoxvilles_joker
  How to make input come after input if certain line inserted and if not runs OtherCode Adrian_L 6 3,339 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
  Picture changing triggered by GPIO q_nerk 2 2,577 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  lxml - etree/lxml need help storing variable for most inserted element goeb 0 2,558 Apr-01-2019, 03:09 AM
Last Post: goeb

Forum Jump:

User Panel Messages

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