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


当前回答

有很多方法可以找到这个最简单的方法就是使用OS包

import os 
print(os.name)

其他回答

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'

使用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)

使用platform.system ()

返回系统/操作系统名称,例如'Linux', 'Darwin', 'Java', 'Windows'。如果无法确定该值,则返回空字符串。

import platform
system = platform.system().lower()

is_windows = system == 'windows'
is_linux = system == 'linux'
is_mac = system == 'darwin'

你也可以使用sys。平台,如果你已经导入了sys,你不想导入另一个模块

>>> import sys
>>> sys.platform
'linux2'

/usr/bin/python3.2

def cls():
    from subprocess import call
    from platform import system

    os = system()
    if os == 'Linux':
        call('clear', shell = True)
    elif os == 'Windows':
        call('cls', shell = True)