什么是在python中获取Windows版本的更好方法?

时间:2012-09-18 06:52:17

标签: python windows operating-system version platform

我打算编写一个执行Windows操作系统版本检查的程序,因为我可以使用sys.windowsversion()[0]或者使用返回字符串而不是int的平台模块来获取Windows发布版本的更好方法?

2 个答案:

答案 0 :(得分:5)

您可以致电sys.getwindowsversion来完成。例如,此输出:

>>> sys.getwindowsversion()
sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')

适用于Windows 7.

来源:http://mail.python.org/pipermail/tutor/2003-November/026227.html

答案 1 :(得分:3)

在C中执行此操作的常用方法是通过_WIN32_WINNT宏,其值记录为heresys.getwindowsversion()可以间接用于确定_WIN32_WINNT,因为它公开了所有必要的位。

import sys

WIN_10 = (10, 0, 0)
WIN_8 = (6, 2, 0)
WIN_7 = (6, 1, 0)
WIN_SERVER_2008 = (6, 0, 1)
WIN_VISTA_SP1 = (6, 0, 1)
WIN_VISTA = (6, 0, 0)
WIN_SERVER_2003_SP2 = (5, 2, 2)
WIN_SERVER_2003_SP1 = (5, 2, 1)
WIN_SERVER_2003 = (5, 2, 0)
WIN_XP_SP3 = (5, 1, 3)
WIN_XP_SP2 = (5, 1, 2)
WIN_XP_SP1 = (5, 1, 1)
WIN_XP = (5, 1, 0)

def get_winver():
    wv = sys.getwindowsversion()
    if hasattr(wv, 'service_pack_major'):  # python >= 2.7
        sp = wv.service_pack_major or 0
    else:
        import re
        r = re.search("\s\d$", wv.service_pack)
        sp = int(r.group(0)) if r else 0
    return (wv.major, wv.minor, sp)

用法:

if get_winver() >= WIN_XP_SP3:
     ...