3

when i type into the Console(CMD) "winver" i will get my windows version (The four numbers left of the build number, example: 1803,1903,1909,2004,20H2) But how can i get my windows version in python? i already tried:

import os
os.system("winver")
input()

But then it will open a new window like in the cmd, but i just want to print the winver without the rest, therefore i did this:

import os
os.system("Reg Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ReleaseId")
input()

But here is the problem that a string is in a string. "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"

How can i fix that? Please help!

1
  • FYI... a string in a string is not a problem: you can escape it ("Reg Query \"...\" /v ReleasedID") or just use another type of quotes, like single quotes: 'Reg Query " ..." /v ReleaseID'. Commented Sep 5, 2023 at 10:22

5 Answers 5

11

you can use platform module

import platform

print(platform.platform())
print(platform.system())
print(platform.release())
print(platform.version())
print(platform.version().split('.')[2]) 
print(platform.machine())

output:

Windows-10-10.0.19041-SP0
Windows
10
10.0.19041
19041
AMD64
Sign up to request clarification or add additional context in comments.

4 Comments

Hello! thanks but that not what i am searching for, it dont shows me the version i want. i already have a solution, but thanks anyway!
But what number will "platform.release()" give when I run in Windows 8.1? "81" or "8.1"? (Note: Windows 7 gives "7" instead of "6.1")
This page shows a list of Windows versions: learn.microsoft.com/en-us/windows/win32/sysinfo/…
in cygwin it print other things :/
3

To put a string inside a string without getting sinttax error you should use single quotes. The code would look like this:

import os
os.system("Reg Query 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion' /v ReleaseId")
input()

1 Comment

Oh yess!! That was what i am searching for thank you so much!
3

You can make use of the sys library, it has a command just for this. Python Docs on sys

import sys
version = sys.getwindowsversion()
print(version)
print(version[2]) # You can directly reference the build element by index number
print(version.build) # Or by name

Output:

sys.getwindowsversion(major=10, minor=0, build=19042, platform=2, service_pack='')
19042
19042

Comments

0

The only registry entry I was able to find that contains the 20H2 value on my system was

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DisplayVersion

Displayversion is also a REG_SZ value.

Unfortunately I don't know since which Windows 10 version this entry exists. I found some info that on older Windows 10 installations this key was an optional 32bit DWORD value that could be used to show build info, edition, WinDir path on the desktop.

But this is definitely the location where winver gets the version info from. If you modify the string winver also shows the modified value.

Comments

0

This script will give the 24H2 -version of the Windows version by using «winreg»

import winreg

def get_windows_version():
    """
    Returns the Windows version in format like "24H2" by reading the DisplayVersion
    from the Windows Registry.
    
    Returns:
        str: The Windows display version (e.g., "24H2", "23H2")
        or "Unknown" if version cannot be determined.
    """
    try:
        # Open the registry key
        key_path = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion"
        reg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path)
        
        # Read the DisplayVersion value
        display_version, _ = winreg.QueryValueEx(reg_key, "DisplayVersion")
        
        # Close the registry key
        winreg.CloseKey(reg_key)
        
        return display_version
    
    except FileNotFoundError:
        return "Unknown - Registry key not found"
    except PermissionError:
        return "Unknown - Access denied to registry"
    except OSError as e:
        return f"Unknown - OS error: {e}"
    except Exception as e:
        return f"Unknown - Error: {e}"

if __name__ == "__main__":
    version = get_windows_version()
    print(f"Windows Version: {version}")

It has quite good error checking too

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.