在很多情况下,需要在python中获取当前系统的类型,用于判断是unix/windows/mac或者java虚拟机等,python中提供了os.name, sys.platform, platform.system等方式
sys
sys.platform会返回当前系统平台的标识符,Linux 是 ‘linux’;Windows 是 ‘win32’ 或者 ‘win64’;macOS 是 ‘darwin’,可以使用startswith()函数来进行判断。
import sys
def get_sys():
if sys.platform.startswith("win"):
print("当前系统是Windows")
return 'windows'
elif sys.platform.startswith("linux")