红联Linux门户
Linux帮助

Python:判断操作系统类型

发布时间:2016-07-25 00:35:48来源:topspeedsnail.com作者:斗大的熊猫
代码为:
from platform import system
def detect_os():
os = system().lower()
if 'darwin' in os:
return "MAC_OS_X"
elif 'windows' in os:
return "WINDOWS"
elif 'linux' in os:
with open('/proc/version','r') as f:
vers = f.read()
if 'microsoft' in vers.lower():
return "WSL" # Windows10的Linux子系统
return "LINUX"
elif 'bsd' in os:
return "BSD"
elif 'cygwin' in os:
return "CYGWIN"
else:
return None
print(detect_os())
 
本文永久更新地址:http://www.linuxdiyf.com/linux/22691.html