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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM