【问题标题】:Python os.name return nt on windows 7Python os.name 在 Windows 7 上返回 nt
【发布时间】:2014-04-14 19:25:33
【问题描述】:

Python os.name 在 Windows 7 上返回“nt”

我正在使用 os.name 来获取当前脚本正在运行的当前操作系统的名称。但奇怪的是,它返回的不是“windows 7”,而是“nt”。

代码如下:

import os

print(os.name)

结果:

nt

【问题讨论】:

  • 没什么奇怪的...根据文档: os.name - "导入的操作系统依赖模块的名称。当前已注册以下名称:'posix', 'nt', ' os2'、'ce'、'java'、'riscos'。"
  • 感谢您的回答。看过但没注意

标签: python


【解决方案1】:

您可以使用platform模块来检查:

In [244]: import platform

In [247]: platform.version()
Out[247]: '6.1.7601'

In [248]: platform.system()
Out[248]: 'Windows'

In [249]: platform.release()
Out[249]: '7'

In [250]: platform.win32_ver()
Out[250]: ('7', '6.1.7601', 'SP1', 'Multiprocessor Free')

In [268]: platform.platform()
Out[268]: 'Windows-7-6.1.7601-SP1'

所以只需使用platform.system() == 'Windows' and platform.release() == 7 来检查;)

或者更简单的'Windows-7' in platform.platform()

【讨论】:

  • 我认为你最好同时检查platform.system()platform.release()...
【解决方案2】:

os-module 让我们可以根据运行代码的操作系统运行不同的代码。

nt 表示您正在运行 windows,并且 posix mac

如果您想检查操作系统是 Windows 还是 Linux 或 OSX,那么最可靠的方法是 platform.system(). 如果您想通过内置 Python 模块 posix 或 nt 进行特定于操作系统的调用,请使用 os.name。

>>> import platform
>>> platform.system()

'Windows' # for Linux it prints 'Linux', Mac it prints `'Darwin'

请参阅此处了解更多信息。 Python: What OS am I running on?

【讨论】:

    【解决方案3】:

    'nt' 的意思是“新技术”,最初是随着 32 位版本的发布而出现的。但在那之后,这个名字就简单地延续了下来,没有任何具体的含义。 更多信息请参考:what is NT?

    【讨论】:

      【解决方案4】:

      根据docos.name 目前有以下之一:'posix'、'nt'、'os2'、'ce'、'java'、'riscos'。你所追求的可能是sys.platform

      【讨论】:

        猜你喜欢
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        • 2013-06-25
        • 1970-01-01
        • 1970-01-01
        • 2012-01-31
        • 2017-07-14
        • 2010-12-17
        相关资源
        最近更新 更多