Python 的platform模块 识别运行环境操作系统

原文: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()


 
 
[python] view plain copy
  1. #!/usr/bin/env python    
  2. #coding=utf-8  
  3.   
  4. #platform_mode.py  
  5.   
  6. import platform  
  7.   
  8. ''''' 
  9.     python中,platform模块给我们提供了很多方法去获取操作系统的信息 
  10.     如: 
  11.         import platform 
  12.         platform.platform()        #获取操作系统名称及版本号,'Linux-3.13.0-46-generic-i686-with-Deepin-2014.2-trusty'   
  13.         platform.version()         #获取操作系统版本号,'#76-Ubuntu SMP Thu Feb 26 18:52:49 UTC 2015' 
  14.         platform.architecture()    #获取操作系统的位数,('32bit', 'ELF') 
  15.         platform.machine()         #计算机类型,'i686' 
  16.         platform.node()            #计算机的网络名称,'XF654' 
  17.         platform.processor()       #计算机处理器信息,''i686' 
  18.         platform.uname()           #包含上面所有的信息汇总,('Linux', 'XF654', '3.13.0-46-generic', '#76-Ubuntu SMP Thu Feb 26 18:52:49 UTC 2015', 'i686', 'i686') 
  19.  
  20.         还可以获得计算机中python的一些信息: 
  21.         import platform 
  22.         platform.python_build() 
  23.         platform.python_compiler() 
  24.         platform.python_branch() 
  25.         platform.python_implementation() 
  26.         platform.python_revision() 
  27.         platform.python_version() 
  28.         platform.python_version_tuple() 
  29. '''  
  30.   
  31. #global var  
  32. #是否显示日志信息  
  33. SHOW_LOG = True  
  34.   
  35. def get_platform():  
  36.     '''''获取操作系统名称及版本号'''  
  37.     return platform.platform()  
  38.   
  39. def get_version():  
  40.     '''''获取操作系统版本号'''  
  41.     return platform.version()  
  42.   
  43. def get_architecture():  
  44.     '''''获取操作系统的位数'''  
  45.     return platform.architecture()  
  46.   
  47. def get_machine():  
  48.     '''''计算机类型'''  
  49.     return platform.machine()  
  50.   
  51. def get_node():  
  52.     '''''计算机的网络名称'''  
  53.     return platform.node()  
  54.   
  55. def get_processor():  
  56.     '''''计算机处理器信息'''  
  57.     return platform.processor()  
  58.   
  59. def get_system():  
  60.     '''''获取操作系统类型'''  
  61.     return platform.system()  
  62.   
  63. def get_uname():  
  64.     '''''汇总信息'''  
  65.     return platform.uname()  
  66.   
  67. def get_python_build():  
  68.     ''''' the Python build number and date as strings'''  
  69.     return platform.python_build()  
  70.   
  71. def get_python_compiler():  
  72.     '''''Returns a string identifying the compiler used for compiling Python'''  
  73.     return platform.python_compiler()  
  74.   
  75. def get_python_branch():  
  76.     '''''Returns a string identifying the Python implementation SCM branch'''  
  77.     return platform.python_branch()  
  78.   
  79. def get_python_implementation():  
  80.     '''''Returns a string identifying the Python implementation. Possible return values are: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’.'''  
  81.     return platform.python_implementation()  
  82.   
  83. def get_python_version():  
  84.     '''''Returns the Python version as string 'major.minor.patchlevel' 
  85.     '''  
  86.     return platform.python_version()  
  87.   
  88. def get_python_revision():  
  89.     '''''Returns a string identifying the Python implementation SCM revision.'''  
  90.     return platform.python_revision()  
  91.   
  92. def get_python_version_tuple():  
  93.     '''''Returns the Python version as tuple (major, minor, patchlevel) of strings'''  
  94.     return platform.python_version_tuple()  
  95.   
  96. def show_os_all_info():  
  97.     '''''打印os的全部信息'''  
  98.     print('获取操作系统名称及版本号 : [{}]'.format(get_platform()))  
  99.     print('获取操作系统版本号 : [{}]'.format(get_version()))  
  100.     print('获取操作系统的位数 : [{}]'.format(get_architecture()))  
  101.     print('计算机类型 : [{}]'.format(get_machine()))  
  102.     print('计算机的网络名称 : [{}]'.format(get_node()))  
  103.     print('计算机处理器信息 : [{}]'.format(get_processor()))  
  104.     print('获取操作系统类型 : [{}]'.format(get_system()))  
  105.     print('汇总信息 : [{}]'.format(get_uname()))  
  106.   
  107. def show_os_info():  
  108.     '''''只打印os的信息,没有解释部分'''  
  109.     print(get_platform())  
  110.     print(get_version())  
  111.     print(get_architecture())  
  112.     print(get_machine())  
  113.     print(get_node())  
  114.     print(get_processor())  
  115.     print(get_system())  
  116.     print(get_uname())  
  117.   
  118. def show_python_all_info():  
  119.     '''''打印python的全部信息'''  
  120.     print('The Python build number and date as strings : [{}]'.format(get_python_build()))  
  121.     print('Returns a string identifying the compiler used for compiling Python : [{}]'.format(get_python_compiler()))  
  122.     print('Returns a string identifying the Python implementation SCM branch : [{}]'.format(get_python_branch()))  
  123.     print('Returns a string identifying the Python implementation : [{}]'.format(get_python_implementation()))  
  124.     print('The version of Python : [{}]'.format(get_python_version()))  
  125.     print('Python implementation SCM revision : [{}]'.format(get_python_revision()))  
  126.     print('Python version as tuple : [{}]'.format(get_python_version_tuple()))  
  127.   
  128. def show_python_info():  
  129.     '''''只打印python的信息,没有解释部分'''  
  130.     print(get_python_build())  
  131.     print(get_python_compiler())  
  132.     print(get_python_branch())  
  133.     print(get_python_implementation())  
  134.     print(get_python_version())  
  135.     print(get_python_revision())  
  136.     print(get_python_version_tuple())  
  137.         
  138. def test():  
  139.     print('操作系统信息:')  
  140.     if SHOW_LOG:  
  141.         show_os_all_info()  
  142.     else:  
  143.         show_os_info()  
  144.     print('#' * 50)  
  145.     print('计算机中的python信息:')  
  146.     if SHOW_LOG:  
  147.         show_python_all_info()  
  148.     else:  
  149.         show_python_info()  
  150.   
  151. def init():  
  152.     global SHOW_LOG  
  153.     SHOW_LOG = True  
  154.       
  155. def main():  
  156.     init()  
  157.     test()  
  158.   
  159. if __name__ == '__main__':  
  160.     main()  
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值