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