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
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()
.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With