26

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
Dzinx
  • 55,586
  • 10
  • 60
  • 78

5 Answers5

42

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.

monkut
  • 42,176
  • 24
  • 124
  • 155
  • 1
    Python 2.5.2 says ('', '', '', '') to platform.win32_ver() in Vista, but Python 2.6 responds 'Vista' properly. Thanks! – Dzinx Oct 14 '08 at 09:03
  • 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 Jul 02 '10 at 20:59
  • As Ahmet indicated, this will not work if you run older versions of Python on Windows 7. Python 2.5.4 returns the following on Windows 7: `platform.release() => 'Vista'`, `platform.win32_ver() => ('', '6.1.7600', '', 'Multiprocessor Free')`. Not surprising given that 2.5 is old, but something to be aware of. – sam Nov 19 '10 at 16:05
  • @sam there's no problem interpreting that, you look at 6.1.7600 and you decide that 6.1 means Windows 7. Vista would be 6.0. – David Heffernan Dec 28 '10 at 20:56
  • 1
    Platform module is all but lovely. Actually I consider it mostly useless: it doesn't give you a portable and usable API to figure out what platform version you're on. – Giampaolo Rodolà Dec 27 '13 at 17:10
  • it is showing Windows 7 with Python 2.7.14 on Server 2008 R2 – sql-noob Mar 15 '19 at 21:05
8

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))
Dzinx
  • 55,586
  • 10
  • 60
  • 78
  • As with all functions in win32api, get it straight from the horse's mouth - http://msdn.microsoft.com/en-us/library/ms724833(VS.85).aspx The returned tuple roughly maps to the fields of OSVERSIONSINFOEX – Jeremy Brown Oct 13 '08 at 12:27
  • 2
    This is wrong too. It will return true for Windows 7 and possibly whatever comes after Windows 7. Vista has version 6.0, Windows 7 has version 6.1. Really people, it's not that hard to get right! – David Heffernan Dec 28 '10 at 20:59
  • @David Heffernan: Check the timestamp of the post, this was WAY before Windows 7 came out :) but thanks for pointing this out. I updated the code to check for minor version number, I think it should suffice this time. – Dzinx Jan 17 '11 at 11:36
  • it all depends whether you want your code to run once, or run again and again. – David Heffernan Jan 17 '11 at 11:52
8

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é
  • 388
  • 1
  • 4
  • 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 Oct 13 '08 at 12:24
  • 2
    I don't understand the up-votes. Windows 7 also has major version equal to 6. If the intent is meant to be isVistaOrLater then the code should say >= 6. So, however you interpret this, it's wrong. – David Heffernan Dec 28 '10 at 20:58
  • @DavidHeffernan This answer had been given one year before Windows7 was released. – Piotr Dobrogost Oct 30 '11 at 18:32
0

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
Deming
  • 1,210
  • 12
  • 15
0
import platform
if platform.release() == "Vista":
    # Do something.

or

import platform
if "Vista" in platform.release():
    # Do something.
Boštjan Mejak
  • 827
  • 9
  • 23