在 Python 中获取 OS 的漂亮名字

Get pretty name of OS in Python

我有 Python 应用程序,我想向用户显示他们操作系统的名称。我知道当前的OS可以这样得到:

import platform
print(platform.platform())

不过这个名字很长。是否有任何标准化的方法可以仅提取 OS 的名称和版本(或 linux 时的分发名称和版本)?而不是这个:

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

我想要这个:

Ubuntu 18.04
Windows 10

或者我必须手动完成 e. G。使用正则表达式?有问题,因为我不知道在 macOS、Fedora 或其他 OS.

上的输出是什么

您在这里使用了错误的工具:platform.platform() 旨在 return 人类可读的名称,而不是可解析的名称。来自文档:

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.

如果您需要 OS 类型及其版本,您最好使用 platform 模块中的其他函数。通常:systemreleaseversion return 单个组件,而 uname return 是一个包含六个属性的命名元组:系统、节点、版本、版本、机器和处理器。