【问题标题】:How to get PhysicalDisk without using WMI in Python?如何在 Python 中不使用 WMI 获取 PhysicalDisk?
【发布时间】:2021-06-22 21:37:10
【问题描述】:

我想获得物理磁盘。我需要的信息之一是 BusType。我想在不使用 WMI 的情况下找到它。有什么办法吗?

感谢您的建议和信息。

【问题讨论】:

    标签: python wmi disk


    【解决方案1】:

    你可以通过powershell获取总线类型的物理磁盘:

    参考:https://www.action1.com/kb/getting-PC-hard-drive-information-using-Powershell.html

    Get-PhysicalDisk
    

    输出:

    Number FriendlyName        SerialNumber                             MediaType CanPool OperationalStatus HealthStatus Usage            Size
    ------ ------------        ------------                             --------- ------- ----------------- ------------ -----            ----
    0      INTEL SSDPEKNW512G8 0000_0000_0100_0000_E4D2_XXXX_XXXX_XXXX. SSD       False   OK                Healthy      Auto-Select 476.94 GB
    

    获取总线类型:

    Get-PhysicalDisk | ft -AutoSize DeviceId,Model,MediaType,BusType,Size
    

    输出:

    DeviceId Model               MediaType BusType         Size
    -------- -----               --------- -------         ----
    0        INTEL SSDPEKNW512G8 SSD       NVMe    512110190592
    

    从python调用powershell:

    参考:https://www.phillipsj.net/posts/executing-powershell-from-python/

    completed = subprocess.run(["powershell", "-Command", "Get-PhysicalDisk | ft -AutoSize DeviceId,Model,MediaType,BusType,Size"], capture_output=True)
    

    示例:

    C:\Users\XXXXXX>python3
    Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import subprocess
    >>> ps_command = "Get-PhysicalDisk | ft -AutoSize DeviceId,Model,MediaType,BusType,Size"
    >>> completed = subprocess.run(["powershell", "-Command", ps_command], capture_output=True)
    >>> print(completed)
    CompletedProcess(args=['powershell', '-Command', 'Get-PhysicalDisk | ft -AutoSize DeviceId,Model,MediaType,BusType,Size'], returncode=0, stdout=b'\r\nDeviceId Model               MediaType BusType         Size\r\n-------- -----               --------- -------         ----\r\n0        INTEL SSDPEKNW512G8 SSD       NVMe    512110190592\r\n\r\n\r\n', stderr=b'')
    >>>
    

    因此你可以制作这样的脚本:

    
    # get_disk_bustype.py
    import subprocess
    ps_command = "Get-PhysicalDisk | ft -AutoSize BusType"
    
    run_result = subprocess.run(["powershell", "-Command", ps_command], capture_output=True)
    print(run_result)
    # CompletedProcess(args=['powershell', '-Command', 'Get-PhysicalDisk | ft -AutoSize BusType'], returncode=0, stdout=b'\r\nBusType\r\n-------\r\nNVMe   \r\n\r\n\r\n', stderr=b'')
    
    run_result_stdout = str(run_result.stdout)
    print(run_result_stdout)
    # '\r\nBusType\r\n-------\r\nNVMe   \r\n\r\n\r\n'
    
    run_result_stdout_bustype = run_result_stdout.split("\\r\\n")[3]
    print(run_result_stdout_bustype)
    # '0        NVMe   '
    
    run_result_stdout_bustype_clean = run_result_stdout_bustype.strip(" ")
    print(run_result_stdout_bustype_clean)
    # 'NVMe'
    
    
    

    输出:

    C:\Users\XXXXX>python3 get_disk_bustype.py
    CompletedProcess(args=['powershell', '-Command', 'Get-PhysicalDisk | ft -AutoSize BusType'], returncode=0, stdout=b'\r\nBusType\r\n-------\r\nNVMe   \r\n\r\n\r\n', stderr=b'')
    b'\r\nBusType\r\n-------\r\nNVMe   \r\n\r\n\r\n'
    NVMe
    NVMe
    

    【讨论】:

    • 谢谢@Kristian。非常感谢。但我希望我可以用 python 本身来做,而不需要调用 powershell 进程。但我不确定它是否可能
    • 您似乎没有指定这必须在 Windows 上,但在 Linux 上您可以使用 psutil python 包,并获得如下输出:>>> psutil.disk_partitions(),输出:[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts=.... .如果设备在 nvme 上,则设备应显示类似 /dev/nvme0p1 的内容,如果在 SATA 上,则应显示 /dev/sda1
    • @Cheries 遗憾的是,在 Windows 中,psutil 设备的输出只是"sdiskpart(device='C:\\\\', mountpoint='C:\\\\', fstype='NTFS', ...,除了驱动器号之外它没有告诉我们任何信息
    • 我使用的是 Windows 而不是 Linux。 @克里斯蒂安
    • 你有什么理由不想要 powershell 调用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多