我想以
的形式在
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
所以你不必费心(当然欢迎提出改进建议)。
试试这个:
>>> 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'