Development

How to Check Operating System Using Python

In this tutorial, you will learn how to determine the current operating system where the python script is running using various methods.
Captain Salem 1 min read
How to Check Operating System Using Python

Python Detect OS - Platform Module

The platform module in Python is part of the standard library. It contains information about the underlying system hardware. We can use the various methods from this module to determine the OS.

Example code is as demonstrated:

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

Output:

Current OS:  Linux

In the code above, we start by importing the platform module. We then call the system() function from the module which gives us the OS name.

If you are running the script on Windows and MacOS, you will get an output as shown, respectively.

'Windows'		[for Windows OS]
'Darwin'		[for mac OS]

Python Detect OS - SYS Module

You are probably familiar with the sys module in Python. This module provides ways of interacting with the running operating system including reading files, environment variables, user information, os information and more.

We can use this module to determine the current OS as shown in the code below:

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

Output:

Current OS:  linux

For other systems, respective output include:

'win32'	->	[Windows: win32]
'darwin' -> [mac OS]

Conclusion

In this tutorial, you came across two simple yet useful methods of determining the current OS using Python. There is not much difference between the two methods, hence use the ones that comes naturally.

If you enjoy our content, please consider buying us a coffee to support our work:

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.