Home » How to Check current Operating System using Python

How to Check current Operating System using Python

In this Tutorial let’s learn about How to Check current Operating System using Python. In order to create a cross-platform program, you may want to analyze the operating system and take a different action depending on whether it is Linux, Mac, Windows, or another.

This tutorial will show you how to obtain system information, including the operating system that is currently in use.

Check Operating System Using the platform Module in Python

The platform module contains information on the underlying system hardware in detail. To determine the name of the operating system, use the following code. The platform module, which includes the built-in system() function is imported here. When called, the system() method returns the name of the operating system.

import platform
os = platform.system()
print("Current OS is : ",os)

The most common outputs are:

  • linux – Linux
  • win32 – Windows
  • darwin – MacOS
import platform

print(platform.system())  # e.g. Windows, Linux, Darwin
print(platform.architecture())  # e.g. 32-bit
print(platform.machine())  # e.g. x86_64
print(platform.processor())  # e.g. i5

Check Operating System Using the sys Module in Python

The sys module can also be used to determine the device’s operating system. To obtain the name of the operating system on our device, we use the platform attribute of the sys module. The sys module features a property sys.platform, which stores the string name of the current operating system type.

import sys
print("Current OS is : ",os)

Python Function for Checking the Operating System

The code initially constructs a platforms dictionary containing the most commonly returned operating system values by sys.platform. If the sys.platform value is not found in the dictionary, the value itself is returned. Otherwise, the platforms dictionary returns a descriptive name for the operating system.

import sys

def get_os():
    platforms = {
        'linux1' : 'Linux',
        'linux2' : 'Linux',
        'darwin' : 'OS X',
        'win32' : 'Windows'
    if sys.platform not in platforms:
        return sys.platform
    return platforms[sys.platform]

print('Current OS :', get_os())

The above python code returns the type of Operating system you are using.


These two modules sys and platform as described above, will assist you in obtaining information about your operating system. There isn’t much of a distinction between sys.platform and platform.

sys runs at run time, whereas sys.platform runs at compile time. As a result, you can utilize any of the ways listed above to get the information you need.

Also Read:

How to Remove Character From String Python
Free Python Books List and Download Links