如何在 Python 中检查操作系统? [英] How do I check the operating system in Python?

查看:17
本文介绍了如何在 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.platformsys.name 粒度更细.

有关有效值,请参阅文档.

另请参阅我在什么操作系统上运行?"

这篇关于如何在 Python 中检查操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆