How can I read system information in Python on Windows?

17,752

Solution 1

There was a similar question asked:

How to get current CPU and RAM usage in Python?

There are quite a few answers telling you how to accomplish this in windows.

Solution 2

In Windows, if you want to get info like from the SYSTEMINFO command, you can use the WMI module.

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

Solution 3

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.

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.

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.

Enjoy!

Share:
17,752
davidmytton
Author by

davidmytton

Co-founder of Console (the best tools for developers). Researching sustainable computing at Imperial College London & Uptime Institute. Previously Co-founder, Server Density (acquired by StackPath).

Updated on August 02, 2022

Comments

  • davidmytton
    davidmytton almost 2 years

    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).