Search code examples
pythonwindowswxpythonwmi

Python find different Windows versions


I am looking to find Windows version using Python script. I need to output version to a variable so I can perform commands based on Windows version. So far i have this code in 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)

I package this code using autopy and it runs correctly on Win10 with the output assigned. When I run the same package on Win7 it doesn't return anything in the 'if' statement for Windows 7. I use Python 3.8 and wxPython 4.0.7 both give me no trouble in running as exe on Win10 or Win7. I just need it to output on Windows 7 "Wrong Windows version" so i know if statement is working on Win7.


Solution

  • In order for Wmi32 output string to assign a variable correctly on Win7 devices you would need to change

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

    to

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

    You will need to add an extra space after Enterprise. This fixed my issue. Now script will run if statement in both Win10 and Win7. https://www.jonathanmedd.net/2012/02/wmi-class-win32_operatingsystem-caption-property-may-contain-a-trailing-space.html