davidmytton
davidmytton

Reputation: 39264

How can I read system information in Python on Windows?

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

Upvotes: 3

Views: 20541

Answers (4)

B.R.
B.R.

Reputation: 334

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

Upvotes: 0

Alex Okrushko
Alex Okrushko

Reputation: 7372

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

Upvotes: 6

AWainb
AWainb

Reputation: 868

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!

Upvotes: 2

Adam Peck
Adam Peck

Reputation: 7045

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.

Upvotes: 2

Related Questions