Python os.name 在 Windows 7 上返回“nt”
我正在使用 os.name 获取运行当前脚本的当前操作系统的名称。但奇怪的是,它返回的不是“windows 7”,而是“nt”。
代码如下:
import os
print(os.name)
结果:
nt
请您参考如下方法:
你可以使用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' 和 platform.release() == 7 来检查 ;)
或者更简单的 platform.platform() 中的“Windows-7”。
