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


Asked by: Maya636 | Posted: 30-11-2021






Answer 1

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

Answered by: Julian401 | Posted: 01-01-2022



Answer 2

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.

Answered by: Madaline572 | Posted: 01-01-2022



Answer 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!

Answered by: Lily171 | Posted: 01-01-2022



Answer 4

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.

Answered by: Maya224 | Posted: 01-01-2022



Similar questions

windows - How to find the file system type in python

I'm looking for a way in python to find out which type of file system is being used for a given path. I'm wanting to do this in a cross platform way. On linux I could just grab the output of df -T but that won't work on OSX or windows.


How to get long file system path from python on Windows

This returns me a short path (DOS convention) (on Windows): import tempfile tempDir = tempfile.mkdtemp() print tempDir Output >>> c:\users\admini~1\appdata\local\temp\tmpf76unv Notice the admini~1. How can I get/convert this to a full path? e.g. C:\users\administrator\appdata...


The system cannot find the file specified windows error python

I want to rename all the files in test folder as 1, 2, 3 and so on import os, sys, path path = r"F:\test" dirs = os.listdir(path) print(dirs) count = 1 for files in dirs: str1 = str(count) os.rename(files, str1) count += 1 but my code giving me this error: WindowsError: [Error 2] The system cannot find the file specified


windows - how to open system apps using OS in python?

This question already has answers here:


windows - How To Add A Plus For My Tabs System In PyQt5 In Python

So, basically I've been working on this web browser in PyQt5 in Python, and recently I found a tabs system, I've tweaked it, and I think it looks pretty nice! But, there is an issue, to be able to open a tab I need to double click, and I can only open around 6 or 7. These are obviously setbacks and I just can't seem to get either to work. Anyways, in this post I would like to know hoe to add a plus sign to add a new tab...


windows - How to find the file system type in python

I'm looking for a way in python to find out which type of file system is being used for a given path. I'm wanting to do this in a cross platform way. On linux I could just grab the output of df -T but that won't work on OSX or windows.


python - svn 0.3.33 module : Windows Error: [Error 2] : The system cannot find the file specified

I have checked out a folder from SVN to my desktop. Actually I need to extract the information like SVN revision no, URL and status for the specific file in the local working copy. Here is the line of code which I am using to extract those info. file = svn.local.LocalClient(filePath[i][j]) fileInfo = file.info() This works perfectly fine in my desktop. But the same thing when I tried to do it in my laptop it throws...


How do I copy files from windows system to any other remote server from python script?

I don't want to use external modules like paramiko or fabric. Is there any python built in module through which we can transfer files from windows. I know for linux scp command is there like this is there any command for windows ?


How to get long file system path from python on Windows

This returns me a short path (DOS convention) (on Windows): import tempfile tempDir = tempfile.mkdtemp() print tempDir Output >>> c:\users\admini~1\appdata\local\temp\tmpf76unv Notice the admini~1. How can I get/convert this to a full path? e.g. C:\users\administrator\appdata...


The system cannot find the file specified windows error python

I want to rename all the files in test folder as 1, 2, 3 and so on import os, sys, path path = r"F:\test" dirs = os.listdir(path) print(dirs) count = 1 for files in dirs: str1 = str(count) os.rename(files, str1) count += 1 but my code giving me this error: WindowsError: [Error 2] The system cannot find the file specified


windows - how to open system apps using OS in python?

This question already has answers here:


windows - How To Add A Plus For My Tabs System In PyQt5 In Python

So, basically I've been working on this web browser in PyQt5 in Python, and recently I found a tabs system, I've tweaked it, and I think it looks pretty nice! But, there is an issue, to be able to open a tab I need to double click, and I can only open around 6 or 7. These are obviously setbacks and I just can't seem to get either to work. Anyways, in this post I would like to know hoe to add a plus sign to add a new tab...


windows - Create an icon in memory with win32 in python

What's a good way to generate an icon in-memory in python? Right now I'm forced to use pygame to draw the icon, then I save it to disk as an .ico file, and then I load it from disk as an ICO resource... Something like this: if os.path.isfile(self.icon): icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE hicon = win32gui.LoadImage(hinst, ...


windows xp - Process size in XP from Python

I have a python script that can approach the 2 GB process limit under Windows XP. On a machine with 2 GB physical memory, that can pretty much lock up the machine, even if the Python script is running at below normal priority. Is there a way in Python to find out my own process size? Thanks, Gerry


How to find out if there is data to be read from stdin on Windows in Python?

This code select.select([sys.stdin], [], [], 1.0) does exactly what I want on Linux, but not in Windows. I've used kbhit() in msvcrt before to see if data is available on stdin for reading, but in this case it always returns 0. Additionally msvcrt.getch() returns '\xff' whereas sys.stdin.read(1) returns ...


In the windows python console, how to make Tab = four spaces?

Hello I would like that when I am in the python console tabbing will give me four spaces. Any ideas?


windows xp - Cygwin and Python 2.6

New to python (and programming). What exactly do I need from Cygwin? I'm running python 2.6 on winxp. Can I safely download the complete Cygwin? It just seems like a huge bundle of stuff. Well, I keep running into modules and functionality (i.e. piping output) which suggest downloading various cygwin components. Will cygwin change or modify any other os functionality or have any other side effects?


windows - Using DPAPI with Python?

Is there a way to use the DPAPI (Data Protection Application Programming Interface) on Windows XP with Python? I would prefer to use an existing module if there is one that can do it. Unfortunately I haven't been able to find a way with Google or Stack Overflow. EDIT: I've taken the example code pointed to by "dF" and tweaked it into a standalone library which can be simply used at a high ...


windows - How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?

I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities. I'd like to create a web page served from one of my systems that would merely present a few buttons which would allow me to initiate these scripts remotely. The problem is that I do...


How do I create a windows service with Python


Python script - SCP on windows

How is it possible to do secure copy using python (windows native install - ActivePython). Unfortunately pexpect module is for unix only and we don't want cygwin locally. I wrote a script that based on pscp.exe win tool - but always stops at first execution becuse of fingerprint host id. and haven't found option to switch this off. the remote hosts are running ssh-server on cygwin (win 2003 servers). Thanks


windows - Example of how to use msilib to create a .msi file from a python module

Can anyone give me an example of how to use python's msilib standard library module to create a msi file from a custom python module? For example, let's say I have a custom module called cool.py with the following code class Cool(object): def print_cool(self): print "cool" and I wa...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top