如何在Windows上阅读Python中的系统信息?

时间:2009-01-22 00:09:44

标签: python windows operating-system

关注此OS-agnostic question,特别是this response,类似于Linux上/ proc / meminfo等数据提供的数据,如何使用Python从Windows读取系统信息(包括但不限于内存使用情况)。

4 个答案:

答案 0 :(得分:4)

在Windows中,如果要从SYSTEMINFO命令获取信息,可以使用WMI module.

import wmi

c = wmi.WMI()    
systeminfo = c.Win32_ComputerSystem()[0]

Manufacturer = systeminfo.Manufacturer
Model = systeminfo.Model

...

类似地,os相关信息可以从osinfo = c.Win32_OperatingSystem()[0]获得 system info is hereos info is here

的完整列表

答案 1 :(得分:2)

您可以尝试使用我之前创建的systeminfo.exe包装器,它有点非正统但它似乎很容易做到并且没有太多代码。

这应该适用于2000 / XP / 2003 Server,并且应该适用于Vista和Win7,前提是它们带有systeminfo.exe并且位于路径上。

import os, re

def SysInfo():
    values  = {}
    cache   = os.popen2("SYSTEMINFO")
    source  = cache[1].read()
    sysOpts = ["Host Name", "OS Name", "OS Version", "Product ID", "System Manufacturer", "System Model", "System type", "BIOS Version", "Domain", "Windows Directory", "Total Physical Memory", "Available Physical Memory", "Logon Server"]

    for opt in sysOpts:
        values[opt] = [item.strip() for item in re.findall("%s:\w*(.*?)\n" % (opt), source, re.IGNORECASE)][0]
    return values

您可以轻松地将其余数据字段附加到sysOpts变量,不包括为其结果提供多行的那些变量,例如CPU&网卡信息。 regexp行的一个简单mod应该能够处理它。

享受!

答案 2 :(得分:1)

有一个类似的问题:

How to get current CPU and RAM usage in Python?

有很多答案告诉你如何在Windows中完成此任务。

答案 3 :(得分:0)

如果操作系统语言不是英语母语,给出的某些答案可能会引起麻烦。我搜索了一种在 systeminfo.exe 周围获取包装器的方法,并找到了以下解决方案。为了使它更舒适,我将结果打包在字典中:

import os
import tempfile

def get_system_info_dict():

    tmp_dir=tempfile.gettempdir()
    file_path=os.path.join(tmp_dir,'out')
    # Call the system command that delivers the needed information
    os.system('powershell -Command gcim WIN32_ComputerSystem -Property * >%s'%file_path)

    with open(file_path,'r') as fh:
        data=fh.read()
    os.remove(file_path)

    data_dict={}
    for line in data.split('\n'):
        try:
            k,v=line.split(':')
        except ValueError:
            continue
        k = k.strip(' ')
        v = v.strip(' ')
        if v!='':
            data_dict[k]=v

    return data_dict

结果字典的每个键都是一个属性(英文!),相关值是为该属性存储的数据。