python - platform.system 和 platform.linux_distribution 究竟输出什么?

标签 python linux

我正在尝试创建一个 python 脚本来在 linux 机器上自动安装和配置某些程序。

我的想法是使用平台和多处理库来询问系统信息(platform.system、platform.linux_distribution、multiprocessing.cpu_count 等),然后安装软件并根据需要为软件配置创建一个文本文件我从这些电话中得到了什么。

我在确定操作系统和发行版时遇到问题:我需要知道脚本在哪个 linux 发行版中运行,以便我可以启动适当的命令,而我要查找的信息不在 python 文档中。

根据该文档 ( http://docs.python.org/2/library/platform.html ), 平台.系统

Returns the system/OS name, e.g. 'Linux', 'Windows', or 'Java'. An empty string is returned if the value cannot be determined.

和 platform.linux_distribution

Tries to determine the name of the Linux OS distribution name.

(...)

Returns a tuple (distname,version,id) which defaults to the args given as parameters. id is the item in parentheses after the version number. It is usually the version codename.

但不够具体,没有提到任何具体案例。

我已经在我的机器上尝试了这些变量(platform.system 返回 'Linux' 和 platform.linux_distribution 'debian'),但我没有办法在其他系统上尝试它(而且我认为创建它不可行每个 Linux 发行版的虚拟机并在每个发行版中尝试)

我需要确切地知道 distname 字符串 platform.linux_distribution 在不同的 linux 发行版中的输出,以便我可以在我的脚本中使用这些值。

我还需要确认 platform.system 是否在每个 linux 发行版中返回相同的东西。这有什么异常(exception)吗?

最佳答案

首先,consulting the code , platform.system 覆盖了 os.uname。后者用于后来的 *nix,是 uname 命令调用。 wiki 中提供了一些可能的值.

其次,linux_distribution roughly speaking ,执行 head/etc/file,其中 file 是以 releaseversion 结尾的第一个文件。即,对于 debian/ubuntu,例如 head/etc/debian_version。它拆分结果行并在找到元组时返回。

我有权访问的基于 Linux 的平台的 linux_distribution 输出示例:

$ python -c 'import platform; print platform.linux_distribution()'
('debian', 'squeeze/sid', '')
('debian', 'wheezy/sid', '')
('SUSE Linux Enterprise Server ', '11', 'x86_64')
('Red Hat Enterprise Linux Server', '5.9', 'Tikanga')
('Red Hat Enterprise Linux Server', '6.4', 'Santiago')

我想你可以在这里找到模式。我个人不会依赖 linux_distribution,有一些已知问题,例如,/etc/debian_version 内容在升级后可能会被破坏。

关于python - platform.system 和 platform.linux_distribution 究竟输出什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20145774/

相关文章:

python - 如何在 wx.Python 或其他 python gui 窗口中打开 HTML JavaScript 页面?

python - selenium.common.exceptions.InsecureCertificateException : probably due to HSTS policy

python - NoReverseMatch at/login - LOGIN_URL 或反向功能出错?

linux - 在 Linux 中使用组处理权限

linux - 内核模块到以太网数据包回显

android - Linux Mint 17.1 上的 ADB 无法识别使用 Android Kitkat 4.4.2 的 Zenfone 5

regex - grep -E {,1} 显示出现次数超过 1 次的结果

linux - 从超过 100 个用户的文件中提取电子邮件

python - 将 BaseHTTPServer 标准输出重定向到日志记录

Python 元素树 : How to add SubElement at VERY specific position?