我想以一种形式在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
所以你不必为此烦恼(当然,欢迎提出改进建议).
解决方法:
如in documentation所述,
platform.system()
返回平台操作系统名称,以便您可以使用它.在this thread中,您还可以看到检查底层操作系统的不同方法.