标识Windows版本
我写打印出详细的Windows版本信息的功能,输出可能是这样一个元组:标识Windows版本
('32bit', 'XP', 'Professional', 'SP3', 'English')
将支持Windows XP及以上。我坚持要获得Windows版本,例如“专业版”,“家庭普通版”等。
platform.win32_ver()或sys.getwindowsversion()不适合我。
win32api.GetVersionEx(1)差点击,但看起来好像没有告诉我足够的信息。
然后我看到了GetProductInfo(),但看起来它没有在pywin32中实现。
任何提示?
您可以使用ctypes
访问任何WinAPI函数。 GetProductInfo()
在windll.kernel32.GetProductInfo
。
我发现MSDN "Getting the System Version" example的Python version(GPL许可,但你可以看到那里的功能的使用)。
太好了!你是怎么发现Python版本? – 2010-05-12 08:18:54
通过在谷歌搜索“windll.kernel32.GetProductInfo”。 – 2010-05-12 08:26:16
如果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])
+ 1为实用答案 – Simon 2011-11-04 13:28:25
我尝试了一些上面的解决方案,但我一直在寻找一些让我“Windows XP”或“Windows 7”的东西。 platform中还有几种方法可以显示更多信息。
import platform
print platform.system(),platform.release()
好像不是Python化,力图使平台相关Python代码... :( – ykatchou 2011-11-03 09:16:12