我需要看些什么来确定我使用的是Windows还是Unix等等?
当前回答
>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'
platform.system()的输出如下:
Linux: Linux 麦克:达尔文 Windows:
参见:平台-访问底层平台的识别数据
其他回答
使用Python区分操作系统的示例代码:
import sys
if sys.platform.startswith("linux"): # could be "linux", "linux2", "linux3", ...
# linux
elif sys.platform == "darwin":
# MAC OS X
elif os.name == "nt":
# Windows, Cygwin, etc. (either 32-bit or 64-bit)
在windows 8上的有趣结果:
>>> import os
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'post2008Server'
编辑:这是一个bug
>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'
platform.system()的输出如下:
Linux: Linux 麦克:达尔文 Windows:
参见:平台-访问底层平台的识别数据
如果你在Windows上使用Cygwin操作系统的os.name是posix,请注意。
>>> import os, platform
>>> print os.name
posix
>>> print platform.system()
CYGWIN_NT-6.3-WOW
Dang -路易斯白兰地打败了我的拳头,但这并不意味着我不能提供你的系统结果为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'