如何检查Python中的操作系统? [英] How do I check the operating system in Python?
本文介绍了如何检查Python中的操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要检查操作系统(在运行脚本的计算机上).
I want to check the operating system (on the computer where the script runs).
我知道我可以在Linux中使用os.system('uname -o')
,但是它会在控制台中显示一条消息,并且我想写一个变量.
I know I can use os.system('uname -o')
in Linux, but it gives me a message in the console, and I want to write to a variable.
该脚本可以判断是Mac,Windows还是Linux都可以.我该如何检查?
It will be okay if the script can tell if it is Mac, Windows or Linux. How can I check it?
推荐答案
您可以使用 sys.platform
:
You can use sys.platform
:
from sys import platform
if platform == "linux" or platform == "linux2":
# linux
elif platform == "darwin":
# OS X
elif platform == "win32":
# Windows...
sys.platform
具有比sys.name
更好的粒度.
有关有效值,请参考文档.
另请参阅我正在运行什么操作系统?"
这篇关于如何检查Python中的操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文