如何获取U盘的容量?

How to get the capacity of a usb pendrive?

我需要获取已卸载的 U 盘的容量。我正在使用 pyudev 来检测它,但我不知道如何获得容量。我阅读了一些有关 pyusb 的文档,但没有发现任何有用的信息。有什么想法吗?

以下摘自 here 的代码运行良好:

def query_hdd_model() -> t.Dict[str, dict]:
    """Get information about all hard drives."""
    if not HDD:
        return {}
    context = pyudev.Context()
    hdds = {}
    for device in context.list_devices(subsystem='block', DEVTYPE='disk'):
        if any(_ in device.device_path for _ in IGNORED_DEVICE_PATHS):
            continue
        hdd = {'size': device.attributes.asint('size')}
        for device_ in itertools.chain([device], device.ancestors):
            try:
                hdd['model'] = device_.attributes.asstring('model')
                break
            except KeyError:
                hdd['model'] = ''
        hdds[device.device_node] = hdd

确切的行如下:

    hdd = {'size': device.attributes.asint('size')}