深圳做企业网站的公司推荐,通河新村街道网站建设,贵州建设网站,wordpress 文章内容模版在很多情况下#xff0c;需要在python中获取当前系统的类型#xff0c;用于判断是unix/windows/mac或者java虚拟机等#xff0c;python中提供了os.name#xff0c; sys.platform#xff0c; platform.system等方式
sys
sys.platform会返回当前系统平台的标识符#xff…在很多情况下需要在python中获取当前系统的类型用于判断是unix/windows/mac或者java虚拟机等python中提供了os.name sys.platform platform.system等方式
sys
sys.platform会返回当前系统平台的标识符Linux 是 ‘linux’Windows 是 ‘win32’ 或者 ‘win64’macOS 是 ‘darwin’可以使用startswith()函数来进行判断。
import sysdef get_sys():if sys.platform.startswith(win):print(当前系统是Windows)return windowselif sys.platform.startswith(linux):print(当前系统是Linux)return linuxelif sys.platform.startswith(darwin):print(当前系统是Mac OS)return macelse:print(当前系统是其他操作系统)return otherplatform
import platformdef get_sys():system platform.system()if system Windows:print(当前系统是Windows)return windowselif system Linux:print(当前系统是Linux)return linuxelif system Darwin:print(当前系统是Mac OS)return macelse:print(当前系统是其他操作系统)return otheros
import os
def get_sys():system os.nameif system nt:print(当前系统是Windows)return windowselif system posix:print(当前系统是Linux或Mac OS)return linuxelif system java:print(当前系统是java虚拟机)return javaelseprint(当前系统是其他操作系统)return other主要区别
sys.platform 在构建配置时指定的编译器定义os.name 用于检查特定的某些模块是否可用例如POSIXnt …platform.system() 实际上运行uname和潜在的几个其他函数来确定运行时的系统类型