簡體   English   中英

Python 查找不同的 Windows 版本

[英]Python find different Windows versions

我正在尋找使用 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。

為了使 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM