Want some programming help? [close]

Get Operating System Info in Python

Introduction

In order to make an application cross-platform, there may be times when you want to inspect the operating system and take a different action depending on whether it is Linux, Mac, Windows, or other.

This guide will show you how to get information about the system including the operating system that is running.

Check operating system

The sys module has a property sys.platform that contains the name of the current operating system type as a string.

import sys

print(sys.platform)

The most common values are:

  • linux - Linux
  • win32 - Windows
  • darwin - MacOS

But other values might include aix, cygwin, or variations of freebsd for example.

Get more details about platform

The platform module provides functions to get more information about a platform.

import platform

print(platform.system())  # e.g. Windows, Linux, Darwin
print(platform.architecture())  # e.g. 64-bit
print(platform.machine())  # e.g. x86_64
print(platform.node())  # Hostname
print(platform.processor())  # e.g. i386

Conclusion

After reading this guide you should understand how to get details about the current operating system in Python.

References

Advertisement