Best practice for throwing an IOError exception in Python

I am writing a small Python interface for reading temperatures from a DS18B20 sensor via sysfs. In addition to temperature, you can also read the state of the CRC sum. To trigger a CRC error, I decided to use IOError:

sn = "28-000005589697"  # sensor serial number
# file containing information of temperature and if CRC is valid: 
fname = "sys/bus/w1/devices/" + sn + "/w1_slave"
with open(fname, 'f') as f: 
...
raise IOError("Invalid CRC in {}!".format(sn))

As I understand it IOError, it has members message, filename, errnoand strerror. In my approach it is used only message. It seems that I missed something.

What is the best practice for throwing an exception IOError, so it goes well with other errors like full disk or file not found, etc.?

+4
source share
1 answer

filename, , CRC. errno strerror libc. IOErrors, . ( ), , .

( strerror):

EIO          5  /* I/O error */
EINVAL      22  /* Invalid argument */

"" libc , .

BTW errno '

+4

Source: https://habr.com/ru/post/1531933/


All Articles