Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to simulate exception for no space left on disk in Python with OpenCV

I am currently writing a wrapper class for OpenCVs implementation of capturing images with a webcam. I want to catch the error exception when the space on disk is full. The code for it would look like the following:

cap = cv2.VideoCapture(cam_idx)
ret, frame = cap.retrieve()


try:
 cv2.imwrite('test.png',frame)
except IOError:
 print("ERROR ON WRITING")

While this will catch the exception, I was wondering if this will also catch other Exceptions and how to only catch the exception for disk full, and what would be the best practice for making a test for this code snippet (besides filling up my disk with so much stuff that the hard drive is really full)

like image 713
Kev1n91 Avatar asked May 20 '18 19:05

Kev1n91


1 Answers

Use OSError and check a result for errno.ENOSPC:

    except OSError as e:
        if e.errno == errno.ENOSPC:

To check free disk space you can use:

psutil.disk_usage(path).free
like image 159
Joss Avatar answered Sep 28 '22 22:09

Joss