0

I have Python app and I would like to show users the name of their operating system. I know that current OS can be obtained as follows:

import platform
print(platform.platform())

However this name is very long. Is there any standardized way to extract only name of OS and version (or name of distribution and version when it's linux)? Instead of this:

Linux-4.15.0-111-generic-x86_64-with-Ubuntu-18.04-bionic
Windows-10-10.0.17763-SP0

I would like this:

Ubuntu 18.04
Windows 10

Or do I have to do it manually e. g. using regex? There is problem, because I don't know what will be the output on macOS, Fedora or other OS.

Michal
  • 1,755
  • 3
  • 21
  • 53
  • Try `print(platform.system())` or `sys.platform` – bigbounty Jul 20 '20 at 06:31
  • Does this answer your question? [How can I find the current OS in Python?](https://stackoverflow.com/questions/110362/how-can-i-find-the-current-os-in-python) Specifically check out the second answer – Tomerikoo Jul 20 '20 at 06:38

1 Answers1

0

You are using the wrong tool here: platform.platform() is intended to return a human readable name, not a parseable one. From the doc:

Returns a single string identifying the underlying platform with as much useful information as possible.

The output is intended to be human readable rather than machine parseable. It may look different on different platforms and this is intended.

You should better use other functions from the platform module if you want the OS type and its version. Typically: system, release and version return individual componenents, while uname returns a named tuple containing six attributes: system, node, release, version, machine, and processor.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252