Identify Windows Editions

2019-07-17 11:47发布

问题:

I am writing a function which prints out detailed Windows Version informations, the output may be a tuple like this:

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

It will be supporting Windows XP and above. And I'm stuck with getting the Windows edition, e.g., "Professional", "Home Basic", etc.

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

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

Then I saw GetProductInfo(), but looks like it's not implemented in pywin32.

Any hints?

回答1:

You can use ctypes to access any WinAPI function. GetProductInfo() is in windll.kernel32.GetProductInfo.

I'd found a Python version (GPL licensed, but you can see usage of the functions there) of the MSDN "Getting the System Version" example.



回答2:

If ctypes doesn't work (due to 32 vs 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])


回答3:

I tried a few of the solutions above, but I was looking for something that gave me "Windows XP" or "Windows 7". There are a few more methods in platform that expose even more information.

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