January 07, 2004
Getting Windows version information in Python

I recently needed to find out which version of Windows on of my scripts was running on. Python 2.3 introduced the os.sys.getwindowsversion() function, which gives you exactly that, but interpreting its output is a bit of a trick. It turns out that it's a fairly thin wrap of GetVersionEx(), and its output is an OSVERSIONINFO structure unpacked into a tuple. So, how's this for a nasty one-liner?

win_version = {(1, 4, 0): "95", (1, 4, 10): "98", (1, 4, 90): "ME", (2, 4, 0): "NT", (2, 5, 0): "2K", (2, 5, 1): "XP"}[os.sys.getwindowsversion()[3], os.sys.getwindowsversion()[0], os.sys.getwindowsversion()[1]]

I suppose I really ought to turn this into a function... ;-)

BTW, I've only tested this on NT and up - if it works (or otherwise) on 9x platforms, I'd love to hear from you.

Posted to Python by Simon Brunning at January 07, 2004 01:05 PM
Comments

> I suppose I really ought to turn this into a function... ;-)

import platform
print platform.release()

Posted by: Fredrik Lundh on January 7, 2004 07:47 PM

> if it works (or otherwise) on 9x platforms, I'd love to hear from you.

>>> import os, sys
>>> os.system('ver')

Windows 98 [Version 4.10.2222]

0
>>> sys.getwindowsversion()
(4, 10, 67766446, 1, ' A ')
>>>

Posted by: Mike Huffman on January 7, 2004 11:56 PM

Simple Nit to pick. sys.getwindowsversion()
works just as well. You are picking up os's
reference to the sys module.

Posted by: Scott David Daniels on January 8, 2004 07:58 PM

Thanks, Fredrik. The platform module is great, in a niche, you-don't-need-it-but-if-you-need-it-you-need-it-bad kind of a way. But it isn't documented *at* *all*.

I've raised a bug for this at sourceforge.

Thanks also to Mike and Scott. getwindowsversion() seems to work as expected on 9x, and the sys module is indeed the right way to get to it.

Posted by: Simon Brunning on January 9, 2004 09:47 AM

A friend of mine has a french win xp pro. your way says to him WP

platform.release() returns "" to him
he has used antispam and such "tools" so he must have accidentaly removed the place where release() seems to look.

So release() doesn't do for windows what you do. I also noticed that On a windows exploer help --> about will give
Microsoft Windows
5.1 (more info)

it won't try the magic that release() [that fail]

I'm having all this in a bug:
http://sourceforge.net/tracker/index.php?func=detail&aid=1180267&group_id=5470&atid=105470

Posted by: Nikos Kouremenos on April 13, 2005 12:08 PM

The way this code is given, if someone just copy pastes and has a user in windows 2003, application Tracebacks with KeyError

this is because 2003 are not included

addition is

(2, 5, 2): '2003',

but then Vista will come along etc etc

I wrote for gajim.org this code: (only showing the windows detection code)

def get_os_info():
if os.name == 'nt':
ver = os.sys.getwindowsversion()
ver_format = ver[3], ver[0], ver[1]
win_version = {
(1, 4, 0): '95',
(1, 4, 10): '98',
(1, 4, 90): 'ME',
(2, 4, 0): 'NT',
(2, 5, 0): '2000',
(2, 5, 1): 'XP',
(2, 5, 2): '2003'
}
if win_version.has_key(ver_format):
return 'Windows' + ' ' + win_version[ver_format]
else:
return 'Windows'

Posted by: Nikos Kouremenos on October 21, 2005 05:59 PM

Cool Solutions, I have one query though
, So how can we find that the given Windows os is a 64 bit or a 32 bit os

Posted by: Rohit on May 21, 2009 04:28 PM
Post a comment
Name:


Email Address:


URL:



Comments:


Remember info?