【问题标题】:Python find different Windows versionsPython 查找不同的 Windows 版本
【发布时间】:2021-09-30 22:48:41
【问题描述】:

我正在寻找使用 Python 脚本的 Windows 版本。我需要将版本输出到一个变量,以便我可以根据 Windows 版本执行命令。到目前为止,我在 python 中有这段代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wmi
import wx

conn = wmi.WMI()
app = wx.App()

for caption in conn.Win32_OperatingSystem():
    print("\nOS:", caption.Caption)

if caption.Caption == 'Microsoft Windows 7 Enterprise':
    print("Wrong Windows version")
    
if caption.Caption == 'Microsoft Windows 10 Enterprise':
    print("This is the correct Windows version")
    
wx.MessageBox(f'OS Version: {caption.Caption}', 'Operating System', wx.OK | wx.ICON_INFORMATION)

我使用 autopy 打包此代码,它在分配了输出的 Win10 上正确运行。当我在 Win7 上运行相同的包时,它不会在 Windows 7 的“if”语句中返回任何内容。我使用 Python 3.8 和 wxPython 4.0.7 都不会在 Win10 或 Win7 上作为 exe 运行。我只需要它在 Windows 7“错误的 Windows 版本”上输出,所以我知道语句是否在 Win7 上运行。

【问题讨论】:

  • 你能在 Windows 7 上打印出caption.Caption 并匹配那个字符串吗?

标签: python windows wxpython wmi


【解决方案1】:

为了让 Wmi32 输出字符串在 Win7 设备上正确分配变量,您需要更改

if caption.Caption == 'Microsoft Windows 7 Enterprise':
    print("Wrong Windows version")

if caption.Caption == 'Microsoft Windows 7 Enterprise ':
    print("Wrong Windows version")

您需要在 Enterprise 之后添加一个额外的空格。这解决了我的问题。现在脚本将在 Win10 和 Win7 中运行 if 语句。 https://www.jonathanmedd.net/2012/02/wmi-class-win32_operatingsystem-caption-property-may-contain-a-trailing-space.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2021-04-04
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 2012-03-21
    • 2019-09-08
    相关资源
    最近更新 更多