¿Cómo comprobar si el sistema operativo es Vista en Python?


¿Cómo, de la manera más simple posible, distinguir entre Windows XP y Windows Vista, utilizando Python y pywin32 o wxPython?

Esencialmente, necesito una función que se llama devolverá Verdadero iff sistema operativo actual es Vista:

>>> isWindowsVista()
True
Author: DzinX, 2008-10-13

5 answers

Python tiene el encantador módulo 'platform' para ayudarte.

>>> import platform
>>> platform.win32_ver()
('XP', '5.1.2600', 'SP2', 'Multiprocessor Free')
>>> platform.system()
'Windows'
>>> platform.version()
'5.1.2600'
>>> platform.release()
'XP'

NOTA: Como se mencionó en los comentarios, los valores apropiados no se pueden devolver cuando se usan versiones anteriores de python.

 41
Author: monkut,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-25 06:57:25

La solución utilizada en Twisted, que no necesita pywin32:

def isVista():
    if getattr(sys, "getwindowsversion", None) is not None:
        return sys.getwindowsversion()[0] == 6
    else:
        return False

Tenga en cuenta que también coincidirá con Windows Server 2008.

 8
Author: Thomas Hervé,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-13 07:55:47

La solución más simple que encontré es esta:

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))
 8
Author: DzinX,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-17 11:34:50

Una idea de http://www.brunningonline.net/simon/blog/archives/winGuiAuto.py.html podría ayudar, que básicamente puede responder a su pregunta:

win_version = {4: "NT", 5: "2K", 6: "XP"}[os.sys.getwindowsversion()[0]]
print "win_version=", win_version
 0
Author: Deming,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-09 14:24:42
import platform
if platform.release() == "Vista":
    # Do something.

O

import platform
if "Vista" in platform.release():
    # Do something.
 0
Author: Boštjan Mejak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-02 18:08:39