How to check if OS is Vista in Python?

前端 未结 5 1184
眼角桃花
眼角桃花 2020-12-08 10:12

How, in the simplest possible way, distinguish between Windows XP and Windows Vista, using Python and pywin32 or wxPython?

Essentially, I need a function that called

相关标签:
5条回答
  • 2020-12-08 10:47

    The simplest solution I found is this one:

    import sys
    
    def isWindowsVista():
        '''Return True iff current OS is Windows Vista.'''
        if sys.platform != "win32":
            return False
        import win32api
        VER_NT_WORKSTATION = 1
        version = win32api.GetVersionEx(1)
        if not version or len(version) < 9:
            return False
        return ((version[0] == 6) and 
                (version[1] == 0) and
                (version[8] == VER_NT_WORKSTATION))
    
    0 讨论(0)
  • 2020-12-08 10:47
    import platform
    if platform.release() == "Vista":
        # Do something.
    

    or

    import platform
    if "Vista" in platform.release():
        # Do something.
    
    0 讨论(0)
  • 2020-12-08 10:49

    An idea from http://www.brunningonline.net/simon/blog/archives/winGuiAuto.py.html might help, which can basically answer your question:

    win_version = {4: "NT", 5: "2K", 6: "XP"}[os.sys.getwindowsversion()[0]]
    print "win_version=", win_version
    
    0 讨论(0)
  • 2020-12-08 10:58

    The solution used in Twisted, which doesn't need pywin32:

    def isVista():
        if getattr(sys, "getwindowsversion", None) is not None:
            return sys.getwindowsversion()[0] == 6
        else:
            return False
    

    Note that it will also match Windows Server 2008.

    0 讨论(0)
  • 2020-12-08 11:10

    Python has the lovely 'platform' module to help you out.

    >>> import platform
    >>> platform.win32_ver()
    ('XP', '5.1.2600', 'SP2', 'Multiprocessor Free')
    >>> platform.system()
    'Windows'
    >>> platform.version()
    '5.1.2600'
    >>> platform.release()
    'XP'
    

    NOTE: As mentioned in the comments proper values may not be returned when using older versions of python.

    0 讨论(0)
提交回复
热议问题