How to check if OS is Vista in Python?

家住魔仙堡 提交于 2019-11-27 01:05:09

问题


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:


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.




回答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.




回答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))



回答4:


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



回答5:


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

or

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


来源:https://stackoverflow.com/questions/196930/how-to-check-if-os-is-vista-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!