Ioctl Linux-specific solution:
import fcntl
import struct
device_path = '/dev/sr0'
req = 0x80081272
buf = ' ' * 8
fmt = 'L'
with open(device_path) as dev:
buf = fcntl.ioctl(dev.fileno(), req, buf)
bytes = struct.unpack('L', buf)[0]
print device_path, 'is about', bytes / (1024 ** 2), 'megabytes'
Other unixes will have different meanings for req, buf, fmt, of course.
source
share