views:

3163

answers:

3

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 will return True iff current OS is Vista:

>>> isWindowsVista()
True
+6  A: 

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[8] == VER_NT_WORKSTATION))
DzinX
As with all functions in win32api, get it straight from the horse's mouth - http://msdn.microsoft.com/en-us/library/ms724833(VS.85).aspxThe returned tuple roughly maps to the fields of OSVERSIONSINFOEX
Jeremy Brown
+8  A: 

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.

Thomas Hervé
Thanks! I don't mind using pywin32 or wxPython (I have them imported anyway), but I would like to be sure that the OS is Vista. I don't know too much about Server 2008 so I wouldn't want my Vista-specific code to run on it.
DzinX
+14  A: 

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'
monkut
Python 2.5.2 says ('', '', '', '') to platform.win32_ver() in Vista, but Python 2.6 responds 'Vista' properly. Thanks!
DzinX
best and clear. please note that your program may not work today since win7 is released and it is also 6.1
Ahmet Alp Balkan