如何识别Python运行的操作系统

953 投票
27 回答
520422 浏览
提问于 2025-04-10 23:35

我需要查看什么来判断我是在Windows系统还是Unix系统等?

27 个回答

149

为了记录,这里是Mac上的结果:

>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Darwin'
>>> platform.release()
'8.11.1'
221

这里是关于 Windows Vista 的系统结果!

>>> import os

>>> os.name
'nt'

>>> import platform

>>> platform.system()
'Windows'

>>> platform.release()
'Vista'

还有 Windows 10 的结果:

>>> import os

>>> os.name
'nt'

>>> import platform

>>> platform.system()
'Windows'

>>> platform.release()
'10'
1265
>>> import os

>>> os.name
'posix'

>>> import platform

>>> platform.system()
'Linux'

>>> platform.release()
'2.6.22-15-generic'

使用 platform.system() 这个命令时,你会得到以下结果:

  • 如果你在用Linux系统,它会显示:Linux
  • 如果你在用Mac系统,它会显示:Darwin
  • 如果你在用Windows系统,它会显示:Windows

想了解更多,可以查看这个链接: platform — 访问底层平台的识别数据

撰写回答