Get the OS Name and Version in Python
In Python, you can get the operating system (OS) and its version using the platform
module from the standard library.
See the following article for how to get the Python version.
Examples in the first part were executed on macOS, with results on Windows and Ubuntu presented later along with OS-specific functions.
All sample code in this article assumes that the platform
, os
, and sys
modules have been imported.
import platform
import os
import sys
Get the OS name: platform.system()
, os.name
, sys.platform
platform.system()
returns the OS name as a string, such as 'Linux'
, 'Darwin'
, 'Java'
, 'Windows'
.
print(platform.system())
# Darwin
The OS name can also be determined using os.name
and sys.platform
. os.name
provides either 'posix'
(Unix-like), 'nt'
(Windows), or 'java'
, while sys.platform
provides values like 'darwin'
, 'linux'
, 'win32'
, etc.
print(os.name)
# posix
print(sys.platform)
# darwin
Results for Windows and Ubuntu are shown later.
Get the OS version: platform.release()
, platform.version()
The OS version can be detected using platform.release()
and platform.version()
, both of which return a string.
platform.release()
provides a simplified version.
print(platform.release())
# 23.0.0
print(platform.version())
# Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:57 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8112
Get the OS information: platform.platform()
platform.platform()
returns a string containing the OS name, version, and other information.
print(platform.platform())
# macOS-14.0-arm64-arm-64bit
Setting the terse
argument to True
returns only minimal information.
print(platform.platform(terse=True))
# macOS-14.0
Additionally, you can also specify the aliased
argument. Although it makes no difference in the example environment, setting it to True
may return an alias for the OS name in some cases.
print(platform.platform(aliased=True))
# macOS-14.0-arm64-arm-64bit
If
aliased
is true, the function will use aliases for various platforms that report system names which differ from their common names, for example SunOS will be reported as Solaris platform.platform()— Python 3.12.1 documentation
Results on macOS, Windows, and Ubuntu
This section shows results on macOS, Windows, and Ubuntu, along with OS-specific functions.
macOS
Results for macOS Sonoma 14.0:
print(platform.system())
# Darwin
print(os.name)
# posix
print(sys.platform)
# darwin
print(platform.release())
# 23.0.0
print(platform.version())
# Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:57 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8112
print(platform.platform())
# macOS-14.0-arm64-arm-64bit
Note that it returns Darwin
rather than macOS
or Sonoma
. For more information, including the latest version numbers and their corresponding macOS names, refer to the Wikipedia page for Darwin.
The platform.mac_ver()
function is specific to macOS, returning a tuple (release, versioninfo, machine)
. In the example environment, versioninfo
is unknown, resulting in an empty string tuple.
print(platform.mac_ver())
# ('14.0', ('', '', ''), 'arm64')
Windows
Results for Windows 11 Home:
print(platform.system())
# Windows
print(os.name)
# nt
print(sys.platform)
# win32
print(platform.release())
# 10
print(platform.version())
# 10.0.22621
print(platform.platform())
# Windows-10-10.0.22621-SP0
The return value of platform.release()
, 10
, is a string, not an integer. Note that even for Windows 11, the version number starts with 10
.
platform.win32_ver()
is a Windows-specific function that returns tuple (release, version, csd, ptype)
. csd
indicates the service pack level.
print(platform.win32_ver())
# ('10', '10.0.22621', 'SP0', 'Multiprocessor Free')
Additionally, platform.win32_edition()
, which returns the Windows edition as a string, and platform.win32_is_iot()
, which returns True
for the IoT edition, were added in Python 3.8.
- platform.win32_edition() — Python 3.12.1 documentation
- platform.win32_is_iot() — Python 3.12.1 documentation
print(platform.win32_edition())
# Core
print(platform.win32_is_iot())
# False
Ubuntu
Results for Ubuntu 22.04.3 LTS:
print(platform.system())
# Linux
print(os.name)
# posix
print(sys.platform)
# linux
print(platform.release())
# 5.15.0-86-generic
print(platform.version())
# #96-Ubuntu SMP Wed Sep 20 08:23:49 UTC 2023
print(platform.platform())
# Linux-5.15.0-86-generic-x86_64-with-glibc2.35
The platform.linux_distribution()
function, which returned the distribution name and version for Unix systems, was removed in Python 3.8.
The distribution name and version can be obtained using the third-party library, distro
, which must be installed separately via pip
.
- distro · PyPI
- python-distro/distro: A much more elaborate replacement for removed Python's
platform.linux_distribution()
method
import distro
print(distro.name())
# Ubuntu
print(distro.id())
# ubuntu
print(distro.version())
# 22.04
Sample code for switching operations based on the OS
You can switch functions or methods based on the OS by using functions like platform.system()
.
For example, get the creation time of a file.
- Get file timestamp in Python (os.stat, os.path.getmtime, and more)
- How to get file creation & modification date/times in Python? - Stack Overflow
def creation_date(path_to_file):
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation.
"""
if platform.system() == 'Windows':
return os.path.getctime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_birthtime
except AttributeError:
# We're probably on Linux. No easy way to get creation dates here,
# so we'll settle for when its content was last modified.
return stat.st_mtime
In this example, platform.system()
is first used to check if the OS is Windows or not, and then exception handling is used to switch operations based on whether the st_birthtime
attribute exists.
For more on exception handling, refer to the following article.