windows 如何在 Python 中检查操作系统是否为 Vista?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/196930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if OS is Vista in Python?
提问by DzinX
How, in the simplest possible way, distinguish between Windows XP and Windows Vista, using Python and pywin32or wxPython?
如何以最简单的方式区分 Windows XP 和 Windows Vista,使用 Python 和pywin32或wxPython?
Essentially, I need a function that called will return True iff current OS is Vista:
本质上,我需要一个被调用的函数,如果当前操作系统是 Vista,它将返回 True:
>>> isWindowsVista()
True
回答by monkut
Python has the lovely 'platform' module to help you out.
Python 有一个可爱的“平台”模块来帮助你。
>>> 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.
注意:如评论中所述,使用旧版本的 python 时可能不会返回正确的值。
回答by DzinX
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))
回答by Thomas Hervé
The solution used in Twisted, which doesn't need pywin32:
Twisted中使用的解决方案,不需要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.
请注意,它也将匹配 Windows Server 2008。
回答by Deming
An idea from http://www.brunningonline.net/simon/blog/archives/winGuiAuto.py.htmlmight help, which can basically answer your question:
来自http://www.brunningonline.net/simon/blog/archives/winGuiAuto.py.html的想法可能会有所帮助,它基本上可以回答您的问题:
win_version = {4: "NT", 5: "2K", 6: "XP"}[os.sys.getwindowsversion()[0]]
print "win_version=", win_version
回答by Bo?tjan Mejak
import platform
if platform.release() == "Vista":
# Do something.
or
或者
import platform
if "Vista" in platform.release():
# Do something.