【问题标题】:Identify Windows Editions识别 Windows 版本
【发布时间】:2011-02-18 11:56:35
【问题描述】:

我正在编写一个打印出详细 Windows 版本信息的函数,输出可能是这样的元组:

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

它将支持 Windows XP 及更高版本。而且我坚持使用 Windows 版本,例如“专业”、“家庭基本”等。

platform.win32_ver() 或 sys.getwindowsversion() 不适合我。

win32api.GetVersionEx(1) 几乎命中,但看起来它没有告诉我足够的信息。

然后看到GetProductInfo(),不过好像pywin32中没有实现。

有什么提示吗?

【问题讨论】:

  • 似乎不是pythonic尝试制作依赖于平台的python代码... :(

标签: python windows windowsversion


【解决方案1】:

您可以使用ctypes 访问任何WinAPI 函数。 GetProductInfo()windll.kernel32.GetProductInfo

我找到了MSDN "Getting the System Version" examplePython version(GPL 许可,但你可以看到那里的函数用法)。

【讨论】:

  • 太棒了!你是怎么找到那个 python 版本的?
  • 通过在 Google 上搜索“windll.kernel32.GetProductInfo”。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我尝试了上面的一些解决方案,但我一直在寻找能够为我提供“Windows XP”或“Windows 7”的解决方案。 platform 中还有更多方法可以公开更多信息。

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

    【讨论】:

      猜你喜欢
      • 2019-05-27
      • 2018-08-02
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2017-06-27
      • 2018-10-25
      相关资源
      最近更新 更多