Identifying Windows Versions

I am writing a function that outputs detailed information about the Windows version, the output can be a tuple like this:

('32bit', 'XP', 'Professional', 'SP3', 'English')

      

It will support Windows XP and above. And I am stuck on Windows edition like "Professional", "Home Basic", etc.

platform.win32_ver () or sys.getwindowsversion () doesn't do this for me.

win32api.GetVersionEx (1) almost hits, but it looks like it doesn't tell me enough information.

Then I saw GetProductInfo () but it looks like it is not implemented in pywin32.

Any hints?

+2


a source to share


3 answers


You can use ctypes

to access any WinAPI function. GetProductInfo()

located in windll.kernel32.GetProductInfo

.



I found the Python version (GPL licensed, but you can see the use of functions there) MSDN example "Getting the system version" .

+3


a source


If ctypes doesn't work (because of 32 or 64 bits?), This hack should:

def get_Windows_name():
    import subprocess, re
    o = subprocess.Popen('systeminfo', stdout=subprocess.PIPE).communicate()[0]
    try: o = str(o, "latin-1")  # Python 3+
    except: pass  
    return re.search("OS Name:\s*(.*)", o).group(1).strip()

print(get_Windows_name())

      

Or just read the registry:



try: import winreg
except: import _winreg as winreg
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion") as key:
    print(winreg.QueryValueEx(key, "EditionID")[0])

      

Or use this:

from win32com.client import GetObject
wim = GetObject('winmgmts:')
print([o.Caption for o in wim.ExecQuery("Select * from Win32_OperatingSystem")][0])

      

+2


a source


I tried several solutions above, but I was looking for something that gave me "Windows XP" or "Windows 7". There are several more methods in the platform that provide additional information.

import platform
print platform.system(),platform.release()

      

+2


a source







All Articles