我需要看些什么来确定我使用的是Windows还是Unix等等?


当前回答

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'

其他回答

>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'

platform.system()的输出如下:

Linux: Linux 麦克:达尔文 Windows:

参见:平台-访问底层平台的识别数据

这个解决方案适用于python和jython。

模块os_identify.py:

import platform
import os

# This module contains functions to determine the basic type of
# OS we are running on.
# Contrary to the functions in the `os` and `platform` modules,
# these allow to identify the actual basic OS,
# no matter whether running on the `python` or `jython` interpreter.

def is_linux():
    try:
        platform.linux_distribution()
        return True
    except:
        return False

def is_windows():
    try:
        platform.win32_ver()
        return True
    except:
        return False

def is_mac():
    try:
        platform.mac_ver()
        return True
    except:
        return False

def name():
    if is_linux():
        return "Linux"
    elif is_windows():
        return "Windows"
    elif is_mac():
        return "Mac"
    else:
        return "<unknown>" 

像这样使用:

import os_identify

print "My OS: " + os_identify.name()

如果你在Windows上使用Cygwin操作系统的os.name是posix,请注意。

>>> import os, platform
>>> print os.name
posix
>>> print platform.system()
CYGWIN_NT-6.3-WOW

我正在使用weblogic附带的WLST工具,它没有实现平台包。

wls:/offline> import os
wls:/offline> print os.name
java 
wls:/offline> import sys
wls:/offline> print sys.platform
'java1.5.0_11'

除了修补系统javaos.py(问题与os.system()在windows 2003与jdk1.5)(我不能这样做,我必须使用weblogic开箱即用),这是我使用的:

def iswindows():
  os = java.lang.System.getProperty( "os.name" )
  return "win" in os.lower()

在windows 8上的有趣结果:

>>> import os
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'post2008Server'

编辑:这是一个bug