我需要看些什么来确定我使用的是Windows还是Unix等等?
当前回答
有很多方法可以找到这个最简单的方法就是使用OS包
import os
print(os.name)
其他回答
使用platform.system ()
返回系统/操作系统名称,例如'Linux', 'Darwin', 'Java', 'Windows'。如果无法确定该值,则返回空字符串。
import platform
system = platform.system().lower()
is_windows = system == 'windows'
is_linux = system == 'linux'
is_mac = system == 'darwin'
像下面这样一个简单的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
在windows 8上的有趣结果:
>>> import os
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'post2008Server'
编辑:这是一个bug
我开始了一个更系统的列表,你可以使用各种模块(随意编辑和添加你的系统):
Linux (64bit) + WSL
x86_64 aarch64
------ -------
os.name posix posix
sys.platform linux linux
platform.system() Linux Linux
sysconfig.get_platform() linux-x86_64 linux-aarch64
platform.machine() x86_64 aarch64
platform.architecture() ('64bit', '') ('64bit', 'ELF')
用archlinux和mint尝试了一下,得到了同样的结果 python2 sys. exe平台的后缀是内核版本,例如linux2,其他的都是相同的 在Windows子系统for Linux上输出相同(尝试使用ubuntu 18.04 LTS),除了platform.architecture() = ('64bit', 'ELF')
窗口(64位)
(在32位子系统中运行32位列)
official python installer 64bit 32bit
------------------------- ----- -----
os.name nt nt
sys.platform win32 win32
platform.system() Windows Windows
sysconfig.get_platform() win-amd64 win32
platform.machine() AMD64 AMD64
platform.architecture() ('64bit', 'WindowsPE') ('64bit', 'WindowsPE')
msys2 64bit 32bit
----- ----- -----
os.name posix posix
sys.platform msys msys
platform.system() MSYS_NT-10.0 MSYS_NT-10.0-WOW
sysconfig.get_platform() msys-2.11.2-x86_64 msys-2.11.2-i686
platform.machine() x86_64 i686
platform.architecture() ('64bit', 'WindowsPE') ('32bit', 'WindowsPE')
msys2 mingw-w64-x86_64-python3 mingw-w64-i686-python3
----- ------------------------ ----------------------
os.name nt nt
sys.platform win32 win32
platform.system() Windows Windows
sysconfig.get_platform() mingw mingw
platform.machine() AMD64 AMD64
platform.architecture() ('64bit', 'WindowsPE') ('32bit', 'WindowsPE')
cygwin 64bit 32bit
------ ----- -----
os.name posix posix
sys.platform cygwin cygwin
platform.system() CYGWIN_NT-10.0 CYGWIN_NT-10.0-WOW
sysconfig.get_platform() cygwin-3.0.1-x86_64 cygwin-3.0.1-i686
platform.machine() x86_64 i686
platform.architecture() ('64bit', 'WindowsPE') ('32bit', 'WindowsPE')
一些评论:
还有distutils.util.get_platform(),它与' sysconfig.get_platform '相同 Windows上的Anaconda与官方的python Windows安装程序相同 我没有Mac电脑,也没有真正的32位系统,也没有上网的动力
要与您的系统进行比较,只需运行这个脚本(如果缺少结果,请在这里附加:)
from __future__ import print_function
import os
import sys
import platform
import sysconfig
print("os.name ", os.name)
print("sys.platform ", sys.platform)
print("platform.system() ", platform.system())
print("sysconfig.get_platform() ", sysconfig.get_platform())
print("platform.machine() ", platform.machine())
print("platform.architecture() ", platform.architecture())
我在游戏中迟到了,但是,以防有人需要它,这是一个我用来调整我的代码的函数,所以它可以在Windows, Linux和MacOs上运行:
import sys
def 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'