【问题标题】:How to get the capacity of a usb pendrive?如何获得usb pendrive的容量?
【发布时间】:2021-03-24 17:58:17
【问题描述】:

我需要获取未安装的 USB 随身碟的容量。我正在使用 pyudev 来检测它,但我不知道如何获得容量。我阅读了一些关于 pyusb 的文档,但我没有发现任何有用的东西。有什么想法吗?

【问题讨论】:

  • 没有,但也许我在 github 上找到了一些东西。 here
  • 链接问题的答案包括通过属性调用检索设备容量等代码。
  • 是的,我明白了,但它使用子进程,我不想使用它。

标签: python-3.x usb pyusb pyudev


【解决方案1】:

以下取自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')}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多