[파이썬] `sys.getwindowsversion()`: 윈도우 버전 반환 (Windows 전용)

If you are developing a Python application specifically for Windows operating system, you might need a way to determine the version of Windows on which your application is running. The sys module in Python provides a convenient function called getwindowsversion() that allows you to retrieve the version of Windows.

Using sys.getwindowsversion()

To use the getwindowsversion() function, you need to import the sys module. Here’s an example code snippet to demonstrate how this function can be used:

import sys

windows_version_info = sys.getwindowsversion()

print(f"Windows Version: {windows_version_info}")
print(f"Major Version: {windows_version_info.major}")
print(f"Minor Version: {windows_version_info.minor}")
print(f"Build Number: {windows_version_info.build}")
print(f"Platform ID: {windows_version_info.platform_id}")
print(f"Service Pack: {windows_version_info.service_pack}")
print(f"Service Pack Major: {windows_version_info.service_pack_major}")
print(f"Service Pack Minor: {windows_version_info.service_pack_minor}")
print(f"Suite Mask: {windows_version_info.suite_mask}")
print(f"Product Type: {windows_version_info.product_type}")
print(f"System Root: {windows_version_info.system_root}")

In this example, we import the sys module and call the getwindowsversion() function, which returns a named tuple containing various attributes related to the Windows version.

We then print out the different attributes of the windows_version_info named tuple, such as the major version, minor version, build number, platform ID, service pack, service pack major and minor versions, suite mask, product type, and the system root.

Example Output

When you run the above code on different versions of Windows, you will get different outputs. Here are a few example outputs:

Windows 10

Windows Version: sys.getwindowsversion(major=10, minor=0, build=19043, platform='Win32NT', service_pack='')
Major Version: 10
Minor Version: 0
Build Number: 19043
Platform ID: Win32NT
Service Pack: 
Service Pack Major: 0
Service Pack Minor: 0
Suite Mask: 512
Product Type: 1
System Root: C:\WINDOWS

Windows 8.1

Windows Version: sys.getwindowsversion(major=6, minor=3, build=9600, platform='Win32NT', service_pack='Service Pack 0')
Major Version: 6
Minor Version: 3
Build Number: 9600
Platform ID: Win32NT
Service Pack: Service Pack 0
Service Pack Major: 0
Service Pack Minor: 0
Suite Mask: 256
Product Type: 1
System Root: C:\WINDOWS

Windows 7

Windows Version: sys.getwindowsversion(major=6, minor=1, build=7601, platform='Win32NT', service_pack='Service Pack 1')
Major Version: 6
Minor Version: 1
Build Number: 7601
Platform ID: Win32NT
Service Pack: Service Pack 1
Service Pack Major: 1
Service Pack Minor: 0
Suite Mask: 272
Product Type: 1
System Root: C:\WINDOWS

Conclusion

The sys.getwindowsversion() function in Python provides a convenient way to retrieve the version of Windows in which your application is running. By knowing the Windows version, you can write code that adapts to specific features or requirements of different Windows versions.

It is important to note that getwindowsversion() is only available on Windows operating system and will raise a NotImplementedError on other platforms. Therefore, it is recommended to add checks to ensure that your application is running on Windows before using this function.