如何在 Windows 上用 Python 读取系统信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/467602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I read system information in Python on Windows?
提问by DavidM
Following from this OS-agnostic question, specifically this response, similar to data available from the likes of /proc/meminfo on Linux, how can I read system information from Windows using Python (including, but not limited to memory usage).
根据这个与操作系统无关的问题,特别是这个响应,类似于 Linux 上的 /proc/meminfo 之类的可用数据,我如何使用 Python 从 Windows 读取系统信息(包括但不限于内存使用)。
采纳答案by Adam Peck
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 中完成此操作。
回答by Alex Okrushko
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 hereand os info is here
同样,操作系统相关信息可以从系统信息osinfo = c.Win32_OperatingSystem()[0]
的完整列表中获得,操作系统信息在这里
回答by AWainb
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. A simple mod to the regexp line should be able to handle that.
您可以轻松地将其余数据字段附加到 sysOpts 变量,不包括那些为其结果提供多行的字段,例如 CPU 和 NIC 信息。regexp 行的简单 mod 应该能够处理。
Enjoy!
享受!