7

Python os.name returns "nt" on windows 7

I'm using os.name to get the name of current operating system under which current script is running. But strangely, instead of "windows 7" it returns "nt".

Here is the code:

import os

print(os.name)

And result:

nt
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • 1
    nothing strange about it…According to the documentation: os.name - "The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'os2', 'ce', 'java', 'riscos'." – skamsie Mar 11 '14 at 09:27
  • take a look at this: http://stackoverflow.com/questions/4553129/when-to-use-os-name-sys-platform-or-platform-system – Jayanth Koushik Mar 11 '14 at 09:28
  • thanks for your answer. I had read that but haven't paid attention to it – Mostafa Talebi Mar 11 '14 at 09:28

4 Answers4

11

You can use platform module to check:

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'

So just use platform.system() == 'Windows' and platform.release() == 7 to check ;)

Or simplier 'Windows-7' in platform.platform().

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • 1
    I think you'd better check both `platform.system()` and `platform.release()` though... – Axel Mar 11 '14 at 09:32
5

The os-module lets us run different code dependent on which operating system the code is running on.

nt means that you are running windows, and posix mac

If you want to check if OS is Windows or Linux or OSX then the most reliable way is platform.system(). If you want to make OS-specific calls but via built-in Python modules posix or nt then use os.name.

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

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

Refer here for more. Python: What OS am I running on?

2

'nt' means 'New Technology' which is come with the release of a 32-bit version initially. But after that, the name carried over simply without any specific meaning. for further information please refer: what is NT?

VENKATESH
  • 31
  • 1
0

According to the doc, os.name currently has one of the following: 'posix', 'nt', 'os2', 'ce', 'java', 'riscos'. What you are after might be sys.platform.

Axel
  • 13,939
  • 5
  • 50
  • 79