python之platform模組
1 #coding:utf-8 2 3 import platform 4 5 t=platform.system() 6 print(t) 7 8 9 10 #coding=utf-8 11 12 #platform_mode.py 13 14 import platform 15 16 ''' 17 python中,platform模組給我們提供了很多方法去獲取作業系統的資訊 18 如: 19 import platform 20 platform.platform() #獲取作業系統名稱及版本號,'Linux-3.13.0-46-generic-i686-with-Deepin-2014.2-trusty'View Code21 platform.version() #獲取作業系統版本號,'#76-Ubuntu SMP Thu Feb 26 18:52:49 UTC 2015' 22 platform.architecture() #獲取作業系統的位數,('32bit', 'ELF') 23 platform.machine() #計算機型別,'i686' 24 platform.node() #計算機的網路名稱,'XF654' 25 platform.processor() #計算機處理器資訊,''i686'26 platform.uname() #包含上面所有的資訊彙總,('Linux', 'XF654', '3.13.0-46-generic', '#76-Ubuntu SMP Thu Feb 26 18:52:49 UTC 2015', 'i686', 'i686') 27 還可以獲得計算機中python的一些資訊: 28 import platform 29 platform.python_build() 30 platform.python_compiler() 31 platform.python_branch()32 platform.python_implementation() 33 platform.python_revision() 34 platform.python_version() 35 platform.python_version_tuple() 36 ''' 37 38 #global var 39 #是否顯示日誌資訊 40 SHOW_LOG = True 41 42 def get_platform(): 43 '''獲取作業系統名稱及版本號''' 44 return platform.platform() 45 46 def get_version(): 47 '''獲取作業系統版本號''' 48 return platform.version() 49 50 def get_architecture(): 51 '''獲取作業系統的位數''' 52 return platform.architecture() 53 54 def get_machine(): 55 '''計算機型別''' 56 return platform.machine() 57 58 def get_node(): 59 '''計算機的網路名稱''' 60 return platform.node() 61 62 def get_processor(): 63 '''計算機處理器資訊''' 64 return platform.processor() 65 66 def get_system(): 67 '''獲取作業系統型別''' 68 return platform.system() 69 70 def get_uname(): 71 '''彙總資訊''' 72 return platform.uname() 73 74 def get_python_build(): 75 ''' the Python build number and date as strings''' 76 return platform.python_build() 77 78 def get_python_compiler(): 79 '''Returns a string identifying the compiler used for compiling Python''' 80 return platform.python_compiler() 81 82 def get_python_branch(): 83 '''Returns a string identifying the Python implementation SCM branch''' 84 return platform.python_branch() 85 86 def get_python_implementation(): 87 '''Returns a string identifying the Python implementation. Possible return values are: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’.''' 88 return platform.python_implementation() 89 90 def get_python_version(): 91 '''Returns the Python version as string 'major.minor.patchlevel' 92 ''' 93 return platform.python_version() 94 95 def get_python_revision(): 96 '''Returns a string identifying the Python implementation SCM revision.''' 97 return platform.python_revision() 98 99 def get_python_version_tuple(): 100 '''Returns the Python version as tuple (major, minor, patchlevel) of strings''' 101 return platform.python_version_tuple() 102 103 def show_os_all_info(): 104 '''列印os的全部資訊''' 105 print('獲取作業系統名稱及版本號 : [{}]'.format(get_platform())) 106 print('獲取作業系統版本號 : [{}]'.format(get_version())) 107 print('獲取作業系統的位數 : [{}]'.format(get_architecture())) 108 print('計算機型別 : [{}]'.format(get_machine())) 109 print('計算機的網路名稱 : [{}]'.format(get_node())) 110 print('計算機處理器資訊 : [{}]'.format(get_processor())) 111 print('獲取作業系統型別 : [{}]'.format(get_system())) 112 print('彙總資訊 : [{}]'.format(get_uname())) 113 114 def show_os_info(): 115 '''只打印os的資訊,沒有解釋部分''' 116 print(get_platform()) 117 print(get_version()) 118 print(get_architecture()) 119 print(get_machine()) 120 print(get_node()) 121 print(get_processor()) 122 print(get_system()) 123 print(get_uname()) 124 125 def show_python_all_info(): 126 '''列印python的全部資訊''' 127 print('The Python build number and date as strings : [{}]'.format(get_python_build())) 128 print('Returns a string identifying the compiler used for compiling Python : [{}]'.format(get_python_compiler())) 129 print('Returns a string identifying the Python implementation SCM branch : [{}]'.format(get_python_branch())) 130 print('Returns a string identifying the Python implementation : [{}]'.format(get_python_implementation())) 131 print('The version of Python : [{}]'.format(get_python_version())) 132 print('Python implementation SCM revision : [{}]'.format(get_python_revision())) 133 print('Python version as tuple : [{}]'.format(get_python_version_tuple())) 134 135 def show_python_info(): 136 '''只打印python的資訊,沒有解釋部分''' 137 print(get_python_build()) 138 print(get_python_compiler()) 139 print(get_python_branch()) 140 print(get_python_implementation()) 141 print(get_python_version()) 142 print(get_python_revision()) 143 print(get_python_version_tuple()) 144 145 def test(): 146 print('作業系統資訊:') 147 if SHOW_LOG: 148 show_os_all_info() 149 else: 150 show_os_info() 151 print('#' * 50) 152 print('計算機中的python資訊:') 153 if SHOW_LOG: 154 show_python_all_info() 155 else: 156 show_python_info() 157 158 def init(): 159 global SHOW_LOG 160 SHOW_LOG = True 161 162 def main(): 163 init() 164 test() 165 166 if __name__ == '__main__': 167 main()
Windows
作業系統資訊:
獲取作業系統名稱及版本號 : [Windows-7-6.1.7601-SP1]
獲取作業系統版本號 : [6.1.7601]
獲取作業系統的位數 : [('32bit', 'WindowsPE')]
計算機型別 : [AMD64]
計算機的網路名稱 : [dw2019]
計算機處理器資訊 : [Intel64 Family 6 Model 69 Stepping 1, GenuineIntel]
獲取作業系統型別 : [Windows]
彙總資訊 : [uname_result(system='Windows', node='dw2019', release='7', version='6.1.7601', machine='AMD64', processor='Intel64 Family 6 Model 69 Stepping 1, GenuineIntel')]
##################################################
計算機中的python資訊:
The Python build number and date as strings : [('v3.3.3:c3896275c0f6', 'Nov 18 2013 21:18:40')]
Returns a string identifying the compiler used for compiling Python : [MSC v.1600 32 bit (Intel)]
Returns a string identifying the Python implementation SCM branch : [v3.3.3]
Returns a string identifying the Python implementation : [CPython]
The version of Python : [3.3.3]
Python implementation SCM revision : [c3896275c0f6]
Python version as tuple : [('3', '3', '3')]
參考大神總結:https://blog.csdn.net/xc_tsao/article/details/44007143