Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中检测Windows 8.1?_Python_Windows - Fatal编程技术网

在Python中检测Windows 8.1?

在Python中检测Windows 8.1?,python,windows,Python,Windows,我们有一个脚本,它使用检测我们各种客户端的OS版本 通过查看platform.py的源代码,我可以看到在Windows系统上,它使用的是sys.getwindowsverion()。不幸的是,在Windows 8.1系统上,该特定函数报告: >>> sys.getwindowsversion() sys.getwindowsversion(major=6, minor=2, build=9200, platform=2, service_pack='') Windows 8.

我们有一个脚本,它使用检测我们各种客户端的OS版本

通过查看platform.py的源代码,我可以看到在Windows系统上,它使用的是sys.getwindowsverion()。不幸的是,在Windows 8.1系统上,该特定函数报告:

>>> sys.getwindowsversion()
sys.getwindowsversion(major=6, minor=2, build=9200, platform=2, service_pack='')
Windows 8.1是6.3.9600:

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Windows\system32>ver

Microsoft Windows [Version 6.3.9600]
因此,我意识到我可以围绕对platform.release()的调用编写一些额外的逻辑,如果返回8,则进行二次检查并尝试运行
ver
,但这似乎有点复杂

有人知道更好的方法吗


运行ActivePython 2.7.2.5以防出现问题

Microsoft更改了版本功能的行为方式。有关更多信息,请参阅

我解决这个问题的方法是使用ctypes和内核模式函数。尽管这是一个内核模式函数,但可以从用户模式调用它。我在很多版本的Windows上都试过了,没有问题

导入ctypes
类OSVERSIONINFOEXW(ctypes.Structure):
_字段\=[('dwosVersionInfo',ctypes.c_ulong),
('dwMajorVersion',ctypes.c_ulong),
('dwMinorVersion',ctypes.c_ulong),
('dwBuildNumber',ctypes.c_ulong),
('dwPlatformId',ctypes.c_ulong),
('szCSDVersion',ctypes.c_wchar*128),
('wServicePackMajor',ctypes.c_ushort),
('wServicePackMinor',ctypes.c_ushort),
('wSuiteMask',ctypes.c_ushort),
('wProductType',ctypes.c_字节),
('wReserved',ctypes.c_字节)]
def get_os_版本():
"""
Get是操作系统的主要版本和次要版本。返回
(大调,小调)。
"""
os_version=OSVERSIONINFOEXW()
os_version.dwosVersionInfo=ctypes.sizeof(os_版本)
retcode=ctypes.windell.Ntdll.RtlGetVersion(ctypes.byref(os_版本))
如果重新编码!=0:
引发异常(“获取操作系统版本失败”)
返回os_version.dwMajorVersion、os_version.dwMinorVersion

您可以从注册表中简单地获取它 HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion 这里有一个值名CurrentVersion Windows 8.1中的数据将是6.3
如果windows决定您应该在win8模式下运行,它将在任何windows平台上运行。现代版的win8应用程序可能没有更新到8.1,但我怀疑你在现代版中运行的是python。哦,胡说八道。Python为此使用。跟随链接:看起来他们正在用一堆无法使用的新函数替换它:-(仅供参考,我在Python bug跟踪器上。同时,做任何有用的事情;-)@TimPeters谢谢你的支持。我听到一些Windows开发人员在工作中抱怨GetVersionEx()的更改,但不确定这是否相关。