python獲得本機硬體資訊

pythontab發表於2013-04-16

注意:這段程式碼需要wmi  和 系統 win32 擴充套件支援。

沒安裝庫的要先下載安裝,我裝的是 WMI-1.4.6.win32 和 pywin32-218.win32-py2.7

還有,程式碼裡面檔案目錄自己修改下咯。

# -*- coding:gb2312 -*-  
import wmi
hardware=file('F:\Python\Hardware.txt','w')
 
w=wmi.WMI()
hardware.write("cpu型號,主頻:\n")
for processor in w.Win32_Processor():          
hardware.write("Processor ID: %s" % processor.DeviceID)
hardware.write("\nProcess Name: %s" % processor.Name.strip()+'\n\n')
hardware.write('記憶體大小:')
totalMemSize=0
for memModule in w.Win32_PhysicalMemory():   
totalMemSize+=int(memModule.Capacity)
hardware.write("\nMemory Capacity: %.2fMB" %((totalMemSize+1048575)/1048576)+'\n\n')
hardware.write('硬碟使用情況:')
for disk in w.Win32_LogicalDisk (DriveType=3):
temp=disk.Caption+" %0.2f%% free" %(100.0 * long (disk.FreeSpace) / long (disk.Size))
hardware.write('\n'+temp)
hardware.write('\n')
hardware.write('\n顯示IP和MAC:\n')
for interface in w.Win32_NetworkAdapterConfiguration (IPEnabled=1):
hardware.write('網路卡驅動資訊:')
hardware.write(interface.Description+'\n')
hardware.write('網路卡MAC地址:')
hardware.write(interface.MACAddress+'\n')
#for ip_address in interface.IPAddress:
hardware.write('IP地址:')
hardware.write(interface.IPAddress[0]+'\n')
hardware.write('外網IP介面')
hardware.write(interface.IPAddress[1]+'\n')
hardware.close()

執行效果圖

python獲得本機硬體資訊


相關文章