如何使用Python检测Mac OS版本? [英] How to detect Mac OS version using Python?

查看:555
本文介绍了如何使用Python检测Mac OS版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定我的应用程序正在Mac OS X系统上运行.但是,我需要做的是弄清楚它运行在哪个版本的Mac OS(或Darwin)上,最好是一个数字.例如,

My application is assumed to be running on a Mac OS X system. However, what I need to do is figure out what version of Mac OS (or Darwin) it is running on, preferably as a number. For instance,

  • "10.4.11"将返回10.4或8
  • "10.5.4"将返回10.5或9
  • "10.6"将返回10.6或10

我发现您可以执行此操作,这在我的系统上返回"8.11.0":

I found out that you could do this, which returns "8.11.0" on my system:

import os
os.system("uname -r")

是否有更清洁的方法来执行此操作,或者至少有一种从结果中提取第一个数字的方法?谢谢!

Is there a cleaner way to do this, or at least a way to pull the first number from the result? Thanks!

推荐答案

>>> import platform
>>> platform.mac_ver()
('10.5.8', ('', '', ''), 'i386')

如您所见,元组的第一项 返回的是字符串,而不是数字(很难将'10 .5.8'转换为数字!-),但是将10.x.y字符串转换为所需的数字类型非常容易.例如,

As you see, the first item of the tuple mac_ver returns is a string, not a number (hard to make '10.5.8' into a number!-), but it's pretty easy to manipulate the 10.x.y string into the kind of numbers you want. For example,

>>> v, _, _ = platform.mac_ver()
>>> v = float('.'.join(v.split('.')[:2]))
>>> print v
10.5

如果您更喜欢Darwin内核版本而不是MacOSX版本,那也很容易访问-使用格式相似的字符串,它是platform.uname()返回的元组的第三项.

If you prefer the Darwin kernel version rather than the MacOSX version, that's also easy to access -- use the similarly-formatted string that's the third item of the tuple returned by platform.uname().

这篇关于如何使用Python检测Mac OS版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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