You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Python获取Windows 11版本时误识别为Win10的问题

Python 3.8.10 识别Windows 11错误的解决方案

错误原因:Python原生的系统版本识别API(如platform模块)默认通过内核主版本号判断Windows版本,而Windows 11的内核主版本号仍为10,和Windows 10一致,因此会出现识别错误。

方案1:调用wmic命令直接读取系统名称

你提到的wmic os get name命令可以直接返回完整的系统标识字符串,包含Windows 11/10的明确标注,Python调用示例如下:

import subprocess

def get_os_name():
    result = subprocess.run(
        ["wmic", "os", "get", "name"],
        capture_output=True,
        text=True
    )
    # 处理输出结果,去掉表头和空行
    output_lines = [line.strip() for line in result.stdout.splitlines() if line.strip()]
    if len(output_lines) >= 2:
        os_full_name = output_lines[1]
        if "Windows 11" in os_full_name:
            return "Windows 11"
        elif "Windows 10" in os_full_name:
            return "Windows 10"
    return "未知Windows版本"

方案2:通过内核Build号判断

Windows 11 所有正式版和预览版的内核Build号均≥22000,Windows 10 最高正式版Build号为19045,可通过该特征直接判断,无需调用外部命令:

import sys

def check_windows_version():
    win_ver = sys.getwindowsversion()
    if win_ver.build >= 22000:
        return "Windows 11"
    else:
        return "Windows 10"

方案3:读取注册表获取系统版本

通过读取Windows注册表中系统版本的官方记录值判断,稳定性更高,调用示例:

import winreg

def get_os_from_reg():
    reg_path = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion"
    with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path) as key:
        product_name = winreg.QueryValueEx(key, "ProductName")[0]
        if "Windows 11" in product_name:
            return "Windows 11"
        else:
            return "Windows 10"

内容的提问来源于stack exchange,提问作者Pedro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 00:30:04