我需要看些什么来确定我使用的是Windows还是Unix等等?
当前回答
像下面这样一个简单的Enum实现怎么样?不需要外部库!
import platform
from enum import Enum
class OS(Enum):
def checkPlatform(osName):
return osName.lower()== platform.system().lower()
MAC = checkPlatform("darwin")
LINUX = checkPlatform("linux")
WINDOWS = checkPlatform("windows") #I haven't test this one
简单地,您可以使用Enum值访问
if OS.LINUX.value:
print("Cool it is Linux")
附注:它是python3
其他回答
你也可以只使用平台模块而不导入os模块来获取所有信息。
>>> import platform
>>> platform.os.name
'posix'
>>> platform.uname()
('Darwin', 'mainframe.local', '15.3.0', 'Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64', 'x86_64', 'i386')
可以使用下面这行代码创建一个漂亮整洁的布局:
for i in zip(['system','node','release','version','machine','processor'],platform.uname()):print i[0],':',i[1]
给出如下输出:
system : Darwin
node : mainframe.local
release : 15.3.0
version : Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64
machine : x86_64
processor : i386
通常缺少的是操作系统版本,但你应该知道如果你运行的是windows, linux或mac,一个平台独立的方法是使用这个测试:
In []: for i in [platform.linux_distribution(),platform.mac_ver(),platform.win32_ver()]:
....: if i[0]:
....: print 'Version: ',i[0]
像下面这样一个简单的Enum实现怎么样?不需要外部库!
import platform
from enum import Enum
class OS(Enum):
def checkPlatform(osName):
return osName.lower()== platform.system().lower()
MAC = checkPlatform("darwin")
LINUX = checkPlatform("linux")
WINDOWS = checkPlatform("windows") #I haven't test this one
简单地,您可以使用Enum值访问
if OS.LINUX.value:
print("Cool it is Linux")
附注:它是python3
同样的,....
import platform
is_windows=(platform.system().lower().find("win") > -1)
if(is_windows): lv_dll=LV_dll("my_so_dll.dll")
else: lv_dll=LV_dll("./my_so_dll.so")
/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)
我正在使用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()