what is better way of getting windows version in python?

前端 未结 2 1443
太阳男子
太阳男子 2021-01-20 02:39

I am going to write a program that performs Windows OS version check, since I can do it with sys.windowsversion()[0] or with platform module that returns string not int what

相关标签:
2条回答
  • 2021-01-20 02:49

    You can do it by calling sys.getwindowsversion. For example this output:

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

    is for Windows 7.

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

    0 讨论(0)
  • 2021-01-20 02:53

    The usual way to do this in C is via _WIN32_WINNT macro, whose values are documented here. sys.getwindowsversion() can indirectly be used to determine _WIN32_WINNT as it exposes all the necessary bits.

    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)
    

    Usage:

    if get_winver() >= WIN_XP_SP3:
         ...
    
    0 讨论(0)
提交回复
热议问题