在本教程中,我们将学习如何在Python中获得当前OS(操作系统)的名称。
使用os.name方法
为了获得操作系统的名称,我们可以使用Python中内置的os.name 方法。
os.name 方法返回用户目前正在使用的底层操作系统名称,例如windows或Linux,mac等。
下面是一个例子。
import os
currentos = os.name
print(currentos)
输出。
'Windows'
在上面的代码中,我们首先导入了os 模块,并在上面调用了一个name 方法来获得当前的操作系统名称。
注意:上述方法对Linux和Mac操作系统返回'POSIX'。
使用 platform.system()
类似地,我们可以使用python中的platform.system() 方法,来获得一个操作系统的名称。
下面是一个例子。
import platform
currentos = platform.system()
print(currentos)
输出。
'Windows'