Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python os.name return nt on windows 7

Tags:

python

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
like image 670
Mostafa Talebi Avatar asked Mar 11 '14 09:03

Mostafa Talebi


2 Answers

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().

like image 125
zhangxaochen Avatar answered Sep 28 '22 09:09

zhangxaochen


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?

like image 24
Arunima Vasudevan Avatar answered Sep 28 '22 09:09

Arunima Vasudevan