最近,想在我的YouMoney(http://code.google.com/p/youmoney/)里面增加提取用戶操作系統版本信息。比如windows用戶,可能要返回Windows XP ,或者Windows 2003, 蘋果用戶應該返回Mac OS X 10.5.8。用了很多辦法,包括在mac系統里調用系統命令,取環境變量,等等。最后無意發現,原來python里里面有個platform模塊就可以干這件事情。省事啊!
mac上這么干
localhost:~ apple$ python
Python 2.5.1 (r251:54863, Jun 17 2009, 20:37:34)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.mac_ver()
('10.5.8', ('', '', ''), 'i386')
>>> platform.version()
'Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386'
>>> platform.platform()
'Darwin-9.8.0-i386-32bit'
>>> platform.system()
'Darwin'
>>>
基本上用platform.platform()就足夠了。如果是在windows上,專門還有個platform.win32_ver() 可用。