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
1
  • 2
    note that if you are only interested windows vs linux etc you can use if os.name=="nt":
    – Ben Page
    Sep 14, 2012 at 12:54

5 Answers 5

43

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.

6
  • 1
    Python 2.5.2 says ('', '', '', '') to platform.win32_ver() in Vista, but Python 2.6 responds 'Vista' properly. Thanks!
    – Dzinx
    Oct 14, 2008 at 9:03
  • best and clear. please note that your program may not work today since win7 is released and it is also 6.1 Jul 2, 2010 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, 2010 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. Dec 28, 2010 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. Dec 27, 2013 at 17:10
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.

3
  • 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, 2008 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. Dec 28, 2010 at 20:58
  • @DavidHeffernan This answer had been given one year before Windows7 was released. Oct 30, 2011 at 18:32
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))
4
  • As with all functions in win32api, get it straight from the horse's mouth - msdn.microsoft.com/en-us/library/ms724833(VS.85).aspx The returned tuple roughly maps to the fields of OSVERSIONSINFOEX Oct 13, 2008 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! Dec 28, 2010 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, 2011 at 11:36
  • it all depends whether you want your code to run once, or run again and again. Jan 17, 2011 at 11:52
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
0
import platform
if platform.release() == "Vista":
    # Do something.

or

import platform
if "Vista" in platform.release():
    # Do something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.