Catch OpenCV Python Exception

I have written a simple motion capture and recording script in Python 3. I am on cv2 version 4.4.0, installed on Ubuntu using “apt install python3-opencv”. My script connects to an IP camera, fetching the URL from a config file. When a host at a URL isn’t reachable, I raise an exception, output a message on the command line, and wait 30 seconds. However, even when catching this error, OpenCV spits out an error on the terminal. I really don’t want to see this error, as I want the terminal to be a clean experience for the user. How can I successfully catch and suppress this error message?

My full code is here on Github.

while True:
          if video is None or not video.isOpened():
            raise ConnectionError
          size=(int(video.get(3)), int(video.get(4)))
          check, frame =


except ConnectionError:
          print("Retrying connection to ",camera_name," in ",str(reconnect_time), " seconds...")

The error message I would like:

Retrying connection to  front_yard  in  30  seconds...

The error message I get:

[tcp @ 0x18d4b40] Connection to tcp:// failed: No route to host
[ERROR:0] global /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap.cpp (140) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.4.0) /tmp/pip-req-build-99ib2vsi/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): in function 'icvExtractPattern'

Retrying connection to  front_yard  in  30  seconds...

create the VideoCapture with apiPreference=cv2.CAP_FFMPEG or something similar. then the backend is fixed and no automatic choice has to happen (that’s what you see going on).