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> I suppose I really ought to turn this into a function... ;-)
import platform
print platform.release()
> 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 ')
>>>
Simple Nit to pick. sys.getwindowsversion()
works just as well. You are picking up os's
reference to the sys module.
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 AMA 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
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'
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