def safe_read(sensor, sensor_name, attribute_name, errors):
    """Reads I2C sensor while catching errors."""
    if sensor is None:
        # There was an error initialising the sensor, can't even try to read it.
        return None
    else:
        try:
            value = getattr(sensor, attribute_name)
        except Exception as err:
            msg = "Error reading {}.{}:\n{}".format(sensor_name, attribute_name, err)
            print(msg)
            errors += (msg + "\n")
            return None
        else:
            print("{}.{} returned {}".format(sensor_name, attribute_name, value))
            return value