如何检查我是否在Python上运行Windows?

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()提供依赖于系统的版本信息.

平台模块提供了详细的检查系统的身份.

  • 'nt'是windows的值 (47认同)
  • @MartinBeckett - 不一定.您可能没有运行Linux的计算机. (21认同)
  • @ArtOfWarfare 你不需要机器;) https://onlinegdb.com/HyhJEzdX4(也许在 2015 年) (3认同)
  • 什么是“java”操作系统?Linux 和 Windows 很有意义,但 Java 不是操作系统,不是吗? (3认同)
  • linux 通常返回什么?posix? (2认同)
  • @AndiJay - 是的,但应该很容易测试! (2认同)
  • 事实证明`os.name`实际上并不是一个好方法。您想使用“platform.system()”代替。这是详细的答案:/sf/answers/4064990681/ (2认同)
  • “...`'posix'`,`'nt'`,`'java'`”当然,Windows可以运行java,所以你答案中的这句话表明测试`'nt'`是一个不好的方法测试您是否在 Windows 上运行。 (2认同)

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正如其他人所提到的那样,可能是要走的路.


对于它的价值,这里有一些他们在platform.py中检查Windows的方法:

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)

  • 在使用 Windows 7(64 位操作系统)的 64 位计算机上,输出如下:Python 3.1.1 (r311:74483, Aug 17 2009, 16:45:59) [MSC v.1500 64 bit (AMD64)] on win32 >>> print(sys.platform) win32 >>> platform.win32_ver() ('post2008Server', '6.1.7100', '', 'Multiprocessor Free') 请注意,构建明确将其称为 win32。 (2认同)

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)

  • 我在64位Windows上,这给了我'win32':) (3认同)

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)