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,
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!
uname() method in python is used to get information about the current operating system. This method returns information like name, release, and version of the current operating system, name of the machine on the network, and hardware identifier in the form of attributes of a tuple-like object.
On your Mac, you'll use the pre-installed Terminal app to view your Python version. Launch Terminal by first opening Spotlight (using the Command+Space shortcut) and then searching for and clicking on “Terminal.” Your current Python version will be displayed.
os.name: The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'java'.
>>> import platform >>> platform.mac_ver() ('10.5.8', ('', '', ''), 'i386')
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
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With