如何在python脚本中查看系统是否为FreeBSD?

How to check whether the system is FreeBSD in a python script?

我想在 python 2.7.x 脚本中以

的形式添加检查
if __check_freebsd__():
    # run actions which would only work on FreeBSD (e.g. create a jail)
elif __check_debian__():
    # run an alternative that runs on Debian-based systems
else:
    raise Error("unsupported OS")

__check_freebsd__ 函数会是什么样子?

我已经为 __check_debian__ 准备了以下代码:

try:
    lsb_release_id_short = sp.check_output([lsb_release, "-d", "-s"]).strip().decode("utf-8")
    ret_value = "Debian" in lsb_release_id_short
    return ret_value
except Exception:
    return False

因此您不必费心(当然欢迎提出改进建议)。

看看os.uname

我不是 100% 确定,但可能类似于 os.uname()[0] == "FreeBSD"

如前所述in documentation,

platform.system()

returns 平台 OS 名称,因此您可以使用它。在 this thread 中,您还可以看到检查底层 OS.

的不同方法

试试这个:

>>> from sys import platform
>>> platform()
# on my system I get
'linux' # check string for freebsd

另外:

# below are my results
>>> import platform
>>> platform.system()
'Linux' # would be 'FreeBSD' if I was using that
>>> platform.platform()
'Linux-3.19.0-15-generic-x86_64-with-Ubuntu-15.04-vivid'