How to access wmi in python?

ryan27968 picture ryan27968 · Jan 12, 2014 · Viewed 14.1k times · Source

So I am trying to access the data from here

in Python. As you can see, it uses wmi. I have tried to use wmi in python before but I am having trouble interpreting the data they are giving me. Please be patient with me as I am a noob to how wmi works. It says that the wmi data is stored in root/OpenHardwareMontor and that it uses two different wmi classes(Hardware and Sensor). But all this information is going over my head.

could someone please give me some sample code to read some data from this?

For example, the code to check cpu core 1 frequency.

EDIT: i have sort of got it working. i run this code:

for Temperature in c.sensor():
    print Temperature.identifier
    print Temperature.value

and i get this:

/hdd/0/load/0
37.6608924866
/intelcpu/0/temperature/1
53.0
/intelcpu/0/temperature/0
42.0
/ram/data/1
2.88324356079
/intelcpu/0/load/2
1.53846144676
/hdd/0/temperature/0
43.0
/intelcpu/0/load/0
2.30768918991
/intelcpu/0/clock/1
1463.29663086
/intelcpu/0/clock/0
133.02696228
/intelcpu/0/clock/2
1463.29663086
/ram/load/0
49.224521637
/ram/data/0
2.79517364502
/intelcpu/0/load/1
3.07692289352

how can i request only the value associated with the identifier /intelcpu/0/temperature/1 ignoring all other values?

Answer

Kobi K picture Kobi K · Jan 12, 2014

The most simple example to use WMI:

c = wmi.WMI()
wql = "Select * From Win32_SerialPort"
for item in c.query(wql):
    print item

Output Example:

instance of Win32_SerialPort
{
    Availability = 2;
    Binary = TRUE;
    Caption = "SpectrumAnalyzer1 (COM15)";
    ConfigManagerErrorCode = 0;
    ConfigManagerUserConfig = FALSE;
    CreationClassName = "Win32_SerialPort";
    Description = "SpectrumAnalyzer1";
    DeviceID = "COM15";
    MaxBaudRate = 128000;
    MaximumInputBufferSize = 0;
    MaximumOutputBufferSize = 0;
    Name = "SpectrumAnalyzer1 (COM15)";
    OSAutoDiscovered = TRUE;
    PNPDeviceID = "USB\\VID_10C4&PID_ED00\\1269376";
    PowerManagementCapabilities = {1};
    PowerManagementSupported = FALSE;
    ProviderType = "RS232 Serial Port";
    SettableBaudRate = TRUE;
    SettableDataBits = TRUE;
    SettableFlowControl = TRUE;
    SettableParity = TRUE;
    SettableParityCheck = TRUE;
    SettableRLSD = TRUE;
    SettableStopBits = TRUE;
    Status = "OK";
    StatusInfo = 3;
    Supports16BitMode = FALSE;
    SupportsDTRDSR = TRUE;
    SupportsElapsedTimeouts = TRUE;
    SupportsIntTimeouts = TRUE;
    SupportsParityCheck = TRUE;
    SupportsRLSD = TRUE;
    SupportsRTSCTS = TRUE;
    SupportsSpecialCharacters = TRUE;
    SupportsXOnXOff = TRUE;
    SupportsXOnXOffSet = TRUE;
    SystemCreationClassName = "Win32_ComputerSystem";
    SystemName = ".......";
};

You can access each item by:

myQuery = c.query(wql)
myQuery.Availability 

Output:

2

For more information, try the WMI cookbook.

Edit #1:

Using if statements and in you can do what you want.

for Temperature in c.sensor():
    if "/intelcpu/0/temperature/1" in Temperature.identifier:
        print Temperature.identifier
        print Temperature.value