原文:http://www.cnblogs.com/itech/archive/2011/01/13/1934653.html
http://blog.csdn.net/xc_tsao/article/details/44007143
python中,platform模块给我们提供了很多方法去获取操作系统的信息
如:
import platform
platform.platform() #获取操作系统名称及版本号,'Linux-3.13.0-46-generic-i686-with-Deepin-2014.2-trusty'
platform.version() #获取操作系统版本号,'#76-Ubuntu SMP Thu Feb 26 18:52:49 UTC 2015'
platform.architecture() #获取操作系统的位数,('32bit', 'ELF')
platform.machine() #计算机类型,'i686'
platform.node() #计算机的网络名称,'XF654'
platform.processor() #计算机处理器信息,''i686'
platform.uname() #包含上面所有的信息汇总,('Linux', 'XF654', '3.13.0-46-generic', '#76-Ubuntu SMP Thu Feb 26 18:52:49 UTC 2015', 'i686', 'i686')
还可以获得计算机中python的一些信息:
import platform
platform.python_build()
platform.python_compiler()
platform.python_branch()
platform.python_implementation()
platform.python_revision()
platform.python_version()
platform.python_version_tuple()
经常地我们需要编写跨平台的脚本,但是由于不同的平台的差异性,我们不得不获得当前所工作的平台(操作系统类型)。
代码如下:
import platform
def TestPlatform():
print ( " ----------Operation System-------------------------- " )
# Windows will be : (32bit, WindowsPE)
# Linux will be : (32bit, ELF)
print (platform.architecture())
# Windows will be : Windows-XP-5.1.2600-SP3 or Windows-post2008Server-6.1.7600
# Linux will be : Linux-2.6.18-128.el5-i686-with-redhat-5.3-Final
print (platform.platform())
# Windows will be : Windows
# Linux will be : Linux
print (platform.system())
print ( " --------------Python Version------------------------- " )
# Windows and Linux will be : 3.1.1 or 3.1.3
print (platform.python_version())
def UsePlatform():
sysstr = platform.system()
if (sysstr == " Windows " ):
print ( " Call Windows tasks " )
elif (sysstr == " Linux " ):
print ( " Call Linux tasks " )
else :
print ( " Other System tasks " )
UsePlatform()