views:

315

answers:

2

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

+1  A: 

There was a similar question asked:

http://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python

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

Adam Peck
A: 

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!

AWainb