How to check if OS is Vista in Python?

17,212

Solution 1

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.

Solution 2

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.

Solution 3

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))
Share:
17,212

Related videos on Youtube

flashrocks
Author by

flashrocks

Updated on April 03, 2020

Comments

  • flashrocks
    flashrocks about 4 years

    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
    
    • Ben Page
      Ben Page over 11 years
      note that if you are only interested windows vs linux etc you can use if os.name=="nt":
  • flashrocks
    flashrocks over 15 years
    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.
  • Jeremy Brown
    Jeremy Brown over 15 years
    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
  • flashrocks
    flashrocks over 15 years
    Python 2.5.2 says ('', '', '', '') to platform.win32_ver() in Vista, but Python 2.6 responds 'Vista' properly. Thanks!
  • ahmet alp balkan
    ahmet alp balkan almost 14 years
    best and clear. please note that your program may not work today since win7 is released and it is also 6.1
  • sam
    sam over 13 years
    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.
  • David Heffernan
    David Heffernan over 13 years
    @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
    David Heffernan over 13 years
    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
    David Heffernan over 13 years
    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!
  • flashrocks
    flashrocks over 13 years
    @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.
  • David Heffernan
    David Heffernan over 13 years
    it all depends whether you want your code to run once, or run again and again.
  • Piotr Dobrogost
    Piotr Dobrogost over 12 years
    @DavidHeffernan This answer had been given one year before Windows7 was released.
  • Giampaolo Rodolà
    Giampaolo Rodolà over 10 years
    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.
  • oybek.t
    oybek.t about 5 years
    it is showing Windows 7 with Python 2.7.14 on Server 2008 R2