How To Get OS Version In Python Script

When I write a Python script, I always need to get the python script running operating system related information, this article will tell you how to get the OS information where you run your Python script.

1. How To Get OS Information In Python Script.

  1. In your Python script file, you can use the Python platform module’s multiple functions to get the information of the current operating system that matches your needs.
  2. First, you should import the Python platform module.
    >>> import platform
  3. The platform.system() function will return the operating system type name.
    >>> platform.system()
    'Windows'
  4. The platform.release() function will return the operating system major release versi0n number.
    >>> platform.release()
    '10'
  5. The platform.version() function will return the detailed release version number (including minor version number) of the current OS.
    >>> platform.version()
    '10.0.19043'
  6. The platform.platform() function will return the more detailed OS information.
    >>> platform.platform()
    'Windows-10-10.0.19043-SP0'
  7. The platform.machine() function will return the current OS machine type.
    >>> platform.machine()
    'AMD64'
  8. The platform.uname() function will return an object that will save all the information of the current operating system such as system name, release number, version string, machine type, processor .etc.
  9. You can get the related attribute value from the returned object as below example.
    >>> uname_result = platform.uname()
    >>>
    >>> uname_result
    uname_result(system='Windows', node='LAPTOP-9FS71VQM', release='10', version='10.0.19043', machine='AMD64', processor='Intel64 Family 6 Model 140 Stepping 1, GenuineIntel')
    >>>
    >>> uname_result.system
    'Windows'
    >>>
    >>> uname_result.release
    '10'
    >>>

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.