1. 程式人生 > >python學習記錄之---------硬體相關(1)獲取電腦的資訊

python學習記錄之---------硬體相關(1)獲取電腦的資訊

工具/版本

(1)安裝環境:Windows7 64bit

(2)使用版本Python3.6

一、準備工作

安裝pywin32模組

安裝wmi模組

二、開始

1、獲取系統版本,版本號等

def sys_version():
    c = wmi.WMI()
    #獲取作業系統版本,版本號
    for sys in c.Win32_OperatingSystem():
        print_err("Version:%s ; Vernum:%s" % (sys.Caption, sys.BuildNumber))
        print_err(sys.OSArchitecture)  #系統是32位還是64位的
        print_err(sys.NumberOfProcesses) #當前系統執行的程序總數
 

Version:Microsoft Windows 7 旗艦版  ; Vernum:7601
64-bit
107

2、獲取CPU ID,CPU名稱,記憶體大小

def cpu_mem():
    c = wmi.WMI()
    # CPU型別和記憶體
    for processor in c.Win32_Processor():
        print_err("Processor ID: %s" % processor.DeviceID)
        print_err("Process Name: %s" % processor.Name.strip())
    for Memory in c.Win32_PhysicalMemory():
        print_err("Memory Capacity: %.fMB" % (int(Memory.Capacity)/1048576))

[2018-08-18 16:23:06]:Processor ID: CPU0
[2018-08-18 16:23:06]: Process Name: Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz
[2018-08-18 16:23:06]: Memory Capacity: 8192MB

3、獲取硬碟資訊

def disk():
    c = wmi.WMI ()
    # 獲取硬碟分割槽
    for physical_disk in c.Win32_DiskDrive():
        for partition in physical_disk.associators("Win32_DiskDriveToDiskPartition"):
            for logical_disk in partition.associators("Win32_LogicalDiskToPartition"):
                print(physical_disk.Caption, partition.Caption, logical_disk.Caption)
    # 獲取硬碟使用百分情況
    for disk in c.Win32_LogicalDisk(DriveType=3):
        print(disk.Caption, "%0.2f%% free" % (100.0 * int(disk.FreeSpace) / int(disk.Size)))

ST1000LM 024 HN-M101MBB SCSI Disk Device Disk #0, Partition #1 C:
ST1000LM 024 HN-M101MBB SCSI Disk Device Disk #0, Partition #2 D:
ST1000LM 024 HN-M101MBB SCSI Disk Device Disk #0, Partition #3 E:
ST1000LM 024 HN-M101MBB SCSI Disk Device Disk #0, Partition #3 F:
C: 68.68% free
D: 43.59% free
E: 70.90% free
F: 21.91% free

3、獲取網絡卡資訊

def network():
    c = wmi.WMI()
    # 獲取MAC和IP地址
    for interface in c.Win32_NetworkAdapterConfiguration(IPEnabled=1):
        print_err("MAC: %s" % interface.MACAddress)
    for ip_address in interface.IPAddress:
        print_err("ip_add: %s" % ip_address)
    # 獲取自啟動程式的位置
    for s in c.Win32_StartupCommand():
        print_err("[%s] %s <%s>" % (s.Location, s.Caption, s.Command))

    # 獲取當前執行的程序
    for process in c.Win32_Process():
        print_err((process.ProcessId, process.Name))