This blog post will show you different ways to find the operating system in Python.
Why would you need to find the operating system name in Python?
I have a few scripts that run cross-platform, on the macOS, Debian Linux and Raspberry Pi OS. A part of the program includes OS-specific code.
For example, if the program is running on macOS, I want it to run Mac-specific osascript
command and prompt me with an alert.
This is how I would do it.
if sys.platform != 'darwin':
osascript = f"""osascript -e 'display notification "Activity Completed" with title "This activity was completed successfully."'"""
Python program to find which operating system is being used
Use os.uname()
to find the operating system
Import the os
module and call the os.uname()
method to find the operating system name.
On Python shell, run this:
import os
os.uname()[1]
Output on Debian Linux:
>>> os.uname()[0] 'Linux'
Output on MacBook Pro with M4 processor:
>>> os.uname()[0] 'Darwin'
Use sys.platform
to find the operating system
Import the sys
module and call the sys.platform
attribute to find the operating system name.
On Python shell, run this:
import sys
sys.platform
Output on Debian Linux:
>>> sys.platform 'linux'
Output on MacBook :
>>> sys.platform 'darwin'
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.