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))
Related videos on Youtube
Author by
flashrocks
Updated on April 03, 2020Comments
-
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 over 11 yearsnote that if you are only interested windows vs linux etc you can use
if os.name=="nt":
-
-
flashrocks over 15 yearsThanks! 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 over 15 yearsAs 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 over 15 yearsPython 2.5.2 says ('', '', '', '') to platform.win32_ver() in Vista, but Python 2.6 responds 'Vista' properly. Thanks!
-
ahmet alp balkan almost 14 yearsbest and clear. please note that your program may not work today since win7 is released and it is also 6.1
-
sam over 13 yearsAs 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 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 over 13 yearsI 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 over 13 yearsThis 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 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 over 13 yearsit all depends whether you want your code to run once, or run again and again.
-
Piotr Dobrogost over 12 years@DavidHeffernan This answer had been given one year before Windows7 was released.
-
Giampaolo Rodolà over 10 yearsPlatform 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 about 5 yearsit is showing Windows 7 with Python 2.7.14 on Server 2008 R2