Home > Mobile >  Determine type of disk on Windows with Python
Determine type of disk on Windows with Python

Time:01-05

I want to list all of my disk and can be able to know its type: "SATA" "NVME" "M.2" or "PCI" on Windows computer.

I made some research with wmi and I get the interface (SCSI or IDE):

c = wmi.WMI()
for disk in c.Win32_DiskDrive():
    print(disk.InterfaceType)

But I don't get the type of the disk. Maybe with the interface I can get the type of my disk ?

Do you have any idea ?

Thx

CodePudding user response:

Query the MSFT_PhysicalDisk class if you insist upon wmi:

import wmi
ws = wmi.WMI(namespace='root/Microsoft/Windows/Storage')
for d in ws.MSFT_PhysicalDisk():
    print(d.BusType, d.MediaType, d.Model)

Sample ouput

 7 3 Elements 1078
11 4 KINGSTON model
11 3 WDC another model

MediaType enumeration:

Value   Meaning
0       Unspecified
3       HDD
4       SSD
5       SCM

Newest BusType enumeration (from WMI Explorer 2.0.0.2):

Value Text                Meaning
----- ----                -------
0     Unknown             The bus type is unknown.
1     SCSI                SCSI
2     ATAPI               ATAPI
3     ATA                 ATA
4     1394                IEEE 1394
5     SSA                 SSA
6     Fibre Channel       Fibre Channel
7     USB                 USB
8     RAID                RAID
9     ISCSI               iSCSI
10    SAS                 Serial Attached SCSI (SAS)
11    SATA                Serial ATA (SATA)
12    SD                  Secure Digital (SD)
13    MMC                 Multimedia Card (MMC)
14    MAX                 This value is reserved for system use.
15    File Backed Virtual File-Backed Virtual
16    Storage Spaces      Storage Spaces
17    NVMe                NVME
18    SCM                 SCM
19    UFS                 UFS
20    reserved            Microsoft reserved

CodePudding user response:

This should get you what you are looking for

import subprocess

result = subprocess.run(['powershell.exe', 'Get-PhysicalDisk | ft -AutoSize DeviceId,Model,MediaType,BusType,Size'], 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE, 
    universal_newlines=True)

print(result.stdout)
  • Related