简体   繁体   English

如何在 Windows 上用 Python 读取系统信息?

[英]How can I read system information in Python on Windows?

根据这个与操作系统无关的问题,特别是这个响应,类似于 Linux 上的 /proc/meminfo 之类的可用数据,我如何使用 Python 从 Windows 读取系统信息(包括但不限于内存使用)。

In Windows, if you want to get info like from the SYSTEMINFO command, you can use the WMI module.在 Windows 中,如果您想从 SYSTEMINFO 命令中获取信息,您可以使用WMI 模块。

import wmi

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

Manufacturer = systeminfo.Manufacturer
Model = systeminfo.Model

... ...

similarly, the os-related info could be got from osinfo = c.Win32_OperatingSystem()[0] the full list of system info is here and os info is here同样,操作系统相关信息可以从osinfo = c.Win32_OperatingSystem()[0]获得系统信息的完整列表在这里操作系统信息在这里

You can try using the systeminfo.exe wrapper I created a while back, it's a bit unorthodox but it seems to do the trick easily enough and without much code.您可以尝试使用我不久前创建的 systeminfo.exe 包装器,它有点非正统,但它似乎很容易完成这个技巧并且没有太多代码。

This should work on 2000/XP/2003 Server, and should work on Vista and Win7 provided they come with systeminfo.exe and it is located on the path.这应该适用于 2000/XP/2003 服务器,并且应该适用于 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

You can easily append the rest of the data fields to the sysOpts variable, excluding those that provide multiple lines for their results, like CPU & NIC information.您可以轻松地将其余数据字段附加到 sysOpts 变量,不包括那些为其结果提供多行的字段,例如 CPU 和 NIC 信息。 A simple mod to the regexp line should be able to handle that. regexp 行的简单 mod 应该能够处理。

Enjoy!享受!

There was a similar question asked:有人问过类似的问题:

How to get current CPU and RAM usage in Python? 如何在 Python 中获取当前的 CPU 和 RAM 使用率?

There are quite a few answers telling you how to accomplish this in windows.有很多答案会告诉您如何在 Windows 中完成此操作。

Some answers given can make trouble if the OS language is not native English.如果操作系统语言不是英语母语,给出的某些答案可能会带来麻烦。 I searched for a way to get a wrapper around the systeminfo.exe and found the following solution.我搜索了一种获取systeminfo.exe包装器的方法,并找到了以下解决方案。 To make it more comfortable I pack the result in a dictionary:为了让它更舒适,我将结果打包在字典中:

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

Each key of a the resulting dictionary is a property (in English!) and the related value is the data stored for the property.结果字典的每个键都是一个属性(英文!),相关值是为该属性存储的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM