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

1 投票
2 回答
1142 浏览
提问于 2025-04-17 05:05

有人知道怎么判断Windows的版本吗?

比如说,对于32位和64位的Windows系统:
- Windows XP 家庭版/专业版
- Windows Vista 商业版/终极版……等等
- Windows 7 基本版/高级版/专业版/终极版……等等

我在想是否可以通过注册表或者Python的接口来获取这些信息呢?

谢谢。

2 个回答

1

看看这个链接:platform.win32_ver()。还有,看看这个问题:如何在Python中检查操作系统是否是Vista?

1

如果ctypes不管用(可能是因为32位和64位的问题?),那么这个小技巧应该能解决:

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

撰写回答