gma*_*man 192 python platform platform-specific
我找到了平台模块,但它说它返回'Windows'并且它在我的机器上返回'Microsoft'.我注意到在stackoverflow的另一个线程中它有时返回'Vista'.
所以,问题是,如何实施?
if isWindows():
...
Run Code Online (Sandbox Code Playgroud)
以前向兼容的方式?如果我必须检查"Vista"之类的东西,那么当下一个版本的Windows出现时它就会中断.
注意:声称这是一个重复的问题的答案实际上没有回答问题isWindows
.他们回答"什么平台"的问题.由于存在许多种类的窗口,它们都没有全面地描述如何得到答案isWindows
.
Mar*_*ett 277
Python os模块
专门针对Python 3.6/3.7:
os.name
:导入的操作系统相关模块的名称.目前已注册以下名称:'posix','nt','java'.
在您的情况下,您要检查'nt'作为os.name
输出:
import os
if os.name == 'nt':
...
Run Code Online (Sandbox Code Playgroud)
还有一个说明os.name
:
另请参见
sys.platform
具有更精细的粒度.os.uname()
提供依赖于系统的版本信息.该平台模块提供了详细的检查系统的身份.
Mar*_*off 50
你在用platform.system
吗?
system() Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. An empty string is returned if the value cannot be determined.
如果这不起作用,也许尝试platform.win32_ver
,如果它没有引发异常,你就在Windows上; 但我不知道它是否向前兼容64位,因为它名称中有32位.
win32_ver(release='', version='', csd='', ptype='') Get additional version information from the Windows Registry and return a tuple (version,csd,ptype) referring to version number, CSD level and OS type (multi/single processor).
但os.name
正如其他人所提到的那样,可能是要走的路.
if sys.platform == 'win32':
#---------
if os.environ.get('OS','') == 'Windows_NT':
#---------
try: import win32api
#---------
# Emulation using _winreg (added in Python 2.0) and
# sys.getwindowsversion() (added in Python 2.3)
import _winreg
GetVersionEx = sys.getwindowsversion
#----------
def system():
""" Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.
An empty string is returned if the value cannot be determined.
"""
return uname()[0]
Run Code Online (Sandbox Code Playgroud)
Eev*_*vee 43
你应该能够依赖os .name.
import os
if os.name == 'nt':
# ...
Run Code Online (Sandbox Code Playgroud)
编辑:现在我想说最明智的方法是通过平台模块,按照另一个答案.
Joc*_*zel 27
在sys中:
import sys
# its win32, maybe there is win64 too?
is_windows = sys.platform.startswith('win')
Run Code Online (Sandbox Code Playgroud)
use*_*246 12
import platform
is_windows = any(platform.win32_ver())
Run Code Online (Sandbox Code Playgroud)
要么
import sys
is_windows = hasattr(sys, 'getwindowsversion')
Run Code Online (Sandbox Code Playgroud)