用 python 检测 OS
Detect OS with python
我四处寻找解决问题的方法,我能找到的最好的方法是:
from sys import platform
if platform == "linux" or platform == "linux2":
# linux
elif platform == "darwin":
# OS X
elif platform == "win32":
# Windows...
有谁知道我如何区分 Linux PC 和 android,因为 android 基于 Linux。如果这是可能的,我怎么能区分 Mac OS 和 iOS
使用platform
模块:
import platform
print(platform.system())
print(platform.release())
print(platform.version())
请注意,Mac 上的系统 运行 将为 platform.system()
return 'Darwin'
platform.platform()
会return 极其详细的数据,比如
'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'
根据我个人的经验,os.uname()
一直是我的最爱。 uname
功能仅存在于基于 Linux 的系统中。以与此类似的方法使用该函数,是检测您是否是 运行 一个 windows 系统的好方法:
import os
try:
test = os.uname()
if test[0] == "Linux":
do something here.
execpt AttributeError:
print("Assuming windows!")
do some other stuff here.
希望对您有所帮助!
您可以查看我的 github 存储库 https://github.com/sk3pp3r/PyOS 并使用 pyos.py 脚本
import platform
plt = platform.system()
if plt == "Windows":
print("Your system is Windows")
# do x y z
elif plt == "Linux":
print("Your system is Linux")
# do x y z
elif plt == "Darwin":
print("Your system is MacOS")
# do x y z
else:
print("Unidentified system")
如果你只想使用标准库,我建议你看看https://docs.python.org/3/library/sys.html#sys.platform。
示例:
import sys
if sys.platform.startswith('linux'):
# Linux specific procedures
elif sys.platform.startswith('darwin'):
# MacOs specific procedures
elif sys.platform.startswith('win32'):
# Windows specific procedures
我四处寻找解决问题的方法,我能找到的最好的方法是:
from sys import platform
if platform == "linux" or platform == "linux2":
# linux
elif platform == "darwin":
# OS X
elif platform == "win32":
# Windows...
有谁知道我如何区分 Linux PC 和 android,因为 android 基于 Linux。如果这是可能的,我怎么能区分 Mac OS 和 iOS
使用platform
模块:
import platform
print(platform.system())
print(platform.release())
print(platform.version())
请注意,Mac 上的系统 运行 将为 platform.system()
platform.platform()
会return 极其详细的数据,比如
'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'
根据我个人的经验,os.uname()
一直是我的最爱。 uname
功能仅存在于基于 Linux 的系统中。以与此类似的方法使用该函数,是检测您是否是 运行 一个 windows 系统的好方法:
import os
try:
test = os.uname()
if test[0] == "Linux":
do something here.
execpt AttributeError:
print("Assuming windows!")
do some other stuff here.
希望对您有所帮助!
您可以查看我的 github 存储库 https://github.com/sk3pp3r/PyOS 并使用 pyos.py 脚本
import platform
plt = platform.system()
if plt == "Windows":
print("Your system is Windows")
# do x y z
elif plt == "Linux":
print("Your system is Linux")
# do x y z
elif plt == "Darwin":
print("Your system is MacOS")
# do x y z
else:
print("Unidentified system")
如果你只想使用标准库,我建议你看看https://docs.python.org/3/library/sys.html#sys.platform。
示例:
import sys
if sys.platform.startswith('linux'):
# Linux specific procedures
elif sys.platform.startswith('darwin'):
# MacOs specific procedures
elif sys.platform.startswith('win32'):
# Windows specific procedures