python判断当前系统是linux、windows还是MacOS

可以使用 Python 的内置模块 sysplatform 来判断当前系统的类型。

示例代码如下:

import sys
import platform

if sys.platform.startswith('linux'):
    print('当前系统为 Linux')
elif sys.platform.startswith('win'):
    print('当前系统为 Windows')
elif sys.platform.startswith('darwin'):
    print('当前系统为 macOS')
else:
    print('无法识别当前系统')

以上代码中,sys.platform 会返回当前系统平台的标识符,例如 Linux 是 'linux',Windows 是 'win32' 或者 'win64',macOS 是 'darwin'。你可以使用 startswith() 函数来进行判断。

sys.platform 返回的字符串是非常明确的,对于不同的平台都有独特的标识符。其中,win32 是 Windows 32 位操作系统的标识符,而 win64 是 Windows 64 位操作系统的标识符。而 darwin 是 macOS 系统的标识符。而使用 startswith() 函数则是来判断字符串是否以某个子串开头的。

 

posted @ 2023-06-12 11:08  No九五二七9527  阅读(4186)  评论(0)    收藏  举报