Python: get name of a USB flash drive device [windows]

前端 未结 3 1477
走了就别回头了
走了就别回头了 2021-01-20 09:14

I\'m trying to write little program which will be able to read some informations about REMOVEABLE_DEVICE (USB). I\'ve tried pyusb but I was not able to pull data I need.

3条回答
  •  悲哀的现实
    2021-01-20 09:54

    I used the approach of @adrianus and improved a bit on it to return multiple usb drives. For how it works check his answer. For quick and dirty code that hopefully works for you check below :)

    def get_usb_volume_name():  # pragma: no cover
        str_computer = "."
        logical_disk_device_ids = []
        volumes = []
        try:
            obj_wmi_service = win32com.client.Dispatch("WbemScripting.SWbemLocator")
            obj_swbem_services = obj_wmi_service.ConnectServer(str_computer, "root\cimv2")
    
        # 1. Win32_DiskDrive
        col_items = obj_swbem_services.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\"")
        for item in col_items:
            disk_drive_device_ids = item.DeviceID.replace('\\', '').replace('.', '')
    
        # 2. Win32_DiskDriveToDiskPartition
        col_items = obj_swbem_services.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition")
        disk_partition_device_ids = []
        for obj_item in col_items:
            for disk_drive_device_id in disk_drive_device_ids:
                if disk_drive_device_id in str(obj_item.Antecedent):
                    disk_partition_device_ids.append(obj_item.Dependent.split('=')[1].replace('"', ''))
                    break
    
        # 3. Win32_LogicalDiskToPartition
        col_items = obj_swbem_services.ExecQuery("SELECT * from Win32_LogicalDiskToPartition")
        for objItem in col_items:
            for disk_partition_device_id in disk_partition_device_ids:
    
                if disk_partition_device_id in str(objItem.Antecedent):
                    logical_disk_device_ids.append(objItem.Dependent.split('=')[1].replace('"', ''))
                    break
    
        # 4. Win32_LogicalDisk
        col_items = []
        for logical_disk_device_id in logical_disk_device_ids:
            col_items.append(obj_swbem_services.ExecQuery("SELECT * from Win32_LogicalDisk WHERE DeviceID=\"" +
                                                          logical_disk_device_id + "\""))
    
        for col_item in col_items:
            volumes.append(col_item[0].VolumeName)
    except IndexError:
        pass
    volumes_result = []
    logical_disk_device_ids_result = []
    for i in range(len(volumes)):
        if volumes[i] != "":
            volumes_result.append(volumes[i])
            logical_disk_device_ids_result.append(logical_disk_device_ids[i])
    
    return logical_disk_device_ids_result, volumes_result
    

提交回复
热议问题