如何使用Squish QT在Python中确定Windows版本?

时间:2011-10-27 09:17:43

标签: python windows registry

任何人都知道如何确定Windows版本?

E.g。对于32位和64位Windows: - Windows XP Home / Professional - Windows Vista Business / Ultimate等 - Windows 7 Home Basic / Home Premium / Professional / Ultimate等等

我想知道是否可以从注册表或Python API中检索此信息?

感谢。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

如果ctypes不起作用(由于32比64比特?),这个hack应该:

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

或者只是阅读注册表:

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

或者使用它:

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