import sys
if sys.platform.startswith("linux"): # could be "linux", "linux2", "linux3", ...# linuxelif sys.platform == "darwin":# MAC OS Xelif sys.platform == "win32":# Windows (either 32-bit or 64-bit)
import platform
print dir(platform)
for x in dir(platform):if x[0].isalnum():try:result = getattr(platform, x)()print "platform."+x+": "+resultexcept TypeError:continue
import platformimport 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 Trueexcept:return False
def is_windows():try:platform.win32_ver()return Trueexcept:return False
def is_mac():try:platform.mac_ver()return Trueexcept: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()
import sysdef get_os(osoptions={'linux':'linux','Windows':'win','macos':'darwin'}):'''get OS to allow code specifics'''opsys = [k for k in osoptions.keys() if sys.platform.lower().find(osoptions[k].lower()) != -1]try:return opsys[0]except:return 'unknown_OS'
from sys import platform
class UnsupportedPlatform(Exception):pass
if "linux" in platform:print("linux")elif "darwin" in platform:print("mac")elif "win" in platform:print("windows")else:raise UnsupportedPlatform