2014-01-12 51 views
5

所以我試圖從Python訪問here如何在python中訪問wmi?

中的數據。正如你所看到的,它使用wmi。我曾嘗試在Python中使用wmi,但我無法解釋他們給我的數據。請耐心等待,因爲我是wmi的工作人員。它說wmi數據存儲在root/OpenHardwareMontor中,它使用兩個不同的wmi類(硬件和傳感器)。但所有這些信息都在我的腦海裏。

有人可能請給我一些示例代碼來從中讀取一些數據?

例如,檢查CPU核心1頻率的代碼。

編輯:我有點得到它的工作。我運行這樣的代碼:

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

,我得到這樣的:

/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 

如何要求與標識符/ intelcpu/0 /溫度/ 1忽略所有其他值相關聯的值?

回答

3

最簡單的例子來使用WMI:

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

輸出示例:

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 = "......."; 
}; 

可以訪問每個item由:

myQuery = c.query(wql) 
myQuery.Availability 

輸出:

2 

欲瞭解更多信息,請嘗試WMI cookbook

編輯#1:

使用if語句和in,你可以做你想做的。

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

好吧,我半決定使用「cockbook」。我跑了這個代碼:對於溫度在c.sensor(): \t print Temperature.name \t print Temperature.value和它的工作,但它打印出每個溫度傳感器的名稱/值。我怎樣才能獲得一個單一的價值? – user1803425

+0

@ user1803425你可以添加輸出到你的文章? –

+0

當我在c.sensor()跑溫度: \t打印Temperature.name \t打印Temperature.value它返回用空間 37.6607666016 CPU內核#2 56.0 CPU內核#1 46.0 可用內存 2.88618087769 CPU內核#2 2.30768918991 溫度 42.0 CPU總 2.30768918991 CPU內核#1 1463.296875 總線速度 133。026992798 CPU內核#2 1463.296875 內存 49.1727905273 使用的內存 2.79223632812 CPU內核#1 2.30768918991 – user1803425