python利用platform模块获取系统信息

 更新时间:2020年10月09日 10:20:49   作者:小杜的学习天地  
这篇文章主要介绍了python利用platform模块获取系统信息,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下

Python platform 模块

platform 模块用于查看当前操作系统的信息,来采集系统版本位数计算机类型名称内核等一系列信息。

使用方法:

#coding:utf-8

import platform

t=platform.system()
print(t)



#coding=utf-8

#platform_mode.py

import platform

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

#global var
#是否显示日志信息
SHOW_LOG = True

def get_platform():
  '''获取操作系统名称及版本号'''
  return platform.platform()

def get_version():
  '''获取操作系统版本号'''
  return platform.version()

def get_architecture():
  '''获取操作系统的位数'''
  return platform.architecture()

def get_machine():
  '''计算机类型'''
  return platform.machine()

def get_node():
  '''计算机的网络名称'''
  return platform.node()

def get_processor():
  '''计算机处理器信息'''
  return platform.processor()

def get_system():
  '''获取操作系统类型'''
  return platform.system()

def get_uname():
  '''汇总信息'''
  return platform.uname()

def get_python_build():
  ''' the Python build number and date as strings'''
  return platform.python_build()

def get_python_compiler():
  '''Returns a string identifying the compiler used for compiling Python'''
  return platform.python_compiler()

def get_python_branch():
  '''Returns a string identifying the Python implementation SCM branch'''
  return platform.python_branch()

def get_python_implementation():
  '''Returns a string identifying the Python implementation. Possible return values are: ‘CPython', ‘IronPython', ‘Jython', ‘PyPy'.'''
  return platform.python_implementation()

def get_python_version():
  '''Returns the Python version as string 'major.minor.patchlevel'
  '''
  return platform.python_version()

def get_python_revision():
  '''Returns a string identifying the Python implementation SCM revision.'''
  return platform.python_revision()

def get_python_version_tuple():
  '''Returns the Python version as tuple (major, minor, patchlevel) of strings'''
  return platform.python_version_tuple()

def show_os_all_info():
  '''打印os的全部信息'''
  print('获取操作系统名称及版本号 : [{}]'.format(get_platform()))
  print('获取操作系统版本号 : [{}]'.format(get_version()))
  print('获取操作系统的位数 : [{}]'.format(get_architecture()))
  print('计算机类型 : [{}]'.format(get_machine()))
  print('计算机的网络名称 : [{}]'.format(get_node()))
  print('计算机处理器信息 : [{}]'.format(get_processor()))
  print('获取操作系统类型 : [{}]'.format(get_system()))
  print('汇总信息 : [{}]'.format(get_uname()))

def show_os_info():
  '''只打印os的信息,没有解释部分'''
  print(get_platform())
  print(get_version())
  print(get_architecture())
  print(get_machine())
  print(get_node())
  print(get_processor())
  print(get_system())
  print(get_uname())

def show_python_all_info():
  '''打印python的全部信息'''
  print('The Python build number and date as strings : [{}]'.format(get_python_build()))
  print('Returns a string identifying the compiler used for compiling Python : [{}]'.format(get_python_compiler()))
  print('Returns a string identifying the Python implementation SCM branch : [{}]'.format(get_python_branch()))
  print('Returns a string identifying the Python implementation : [{}]'.format(get_python_implementation()))
  print('The version of Python : [{}]'.format(get_python_version()))
  print('Python implementation SCM revision : [{}]'.format(get_python_revision()))
  print('Python version as tuple : [{}]'.format(get_python_version_tuple()))

def show_python_info():
  '''只打印python的信息,没有解释部分'''
  print(get_python_build())
  print(get_python_compiler())
  print(get_python_branch())
  print(get_python_implementation())
  print(get_python_version())
  print(get_python_revision())
  print(get_python_version_tuple())

def test():
  print('操作系统信息:')
  if SHOW_LOG:
    show_os_all_info()
  else:
    show_os_info()
  print('#' * 50)
  print('计算机中的python信息:')
  if SHOW_LOG:
    show_python_all_info()
  else:
    show_python_info()

def init():
  global SHOW_LOG
  SHOW_LOG = True

def main():
  init()
  test()

if __name__ == '__main__':
  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')]

以上就是python利用platform模块获取系统信息的详细内容,更多关于Python platform 模块的资料请关注脚本之家其它相关文章!

相关文章

  • python二叉树类以及其4种遍历方法实例

    python二叉树类以及其4种遍历方法实例

    二叉树是一种特殊的树,最直观地体现于它的每个节点至多有两个子节点,二叉树是非常实用的一种数据结构,常常用于实现二叉查找树及二叉堆等,下面这篇文章主要给大家介绍了关于python二叉树类以及其4种遍历方法的相关资料,需要的朋友可以参考下
    2022-05-05
  • Django REST为文件属性输出完整URL的方法

    Django REST为文件属性输出完整URL的方法

    这篇文章主要给大家介绍了关于Django REST如何为文件属性输出完整URL的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用django具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-12-12
  • Python可视化神器pyecharts绘制折线图详情

    Python可视化神器pyecharts绘制折线图详情

    这篇文章主要介绍了Python可视化神器pyecharts绘制折线图详情,折线图和柱状图一样是我们日常可视化最多的一个图例,当然它的优势和适用场景相信大家肯定不陌生,要想快速的得出趋势,抓住趋势二字,就会很快的想到要用折线图来表示了
    2022-07-07
  • Python Scrapy框架:通用爬虫之CrawlSpider用法简单示例

    Python Scrapy框架:通用爬虫之CrawlSpider用法简单示例

    这篇文章主要介绍了Python Scrapy框架:通用爬虫之CrawlSpider用法,结合实例形式分析了Scrapy框架中CrawlSpider的基本使用方法,需要的朋友可以参考下
    2020-04-04
  • python3报错check_hostname requires server_hostname的解决

    python3报错check_hostname requires server_hostname的解决

    这篇文章主要介绍了python3报错check_hostname requires server_hostname的解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • python状态机transitions库详解

    python状态机transitions库详解

    在用python做一个比较复杂的小项目,需要根据不同的输入,控制摄像头采集执行不同的任务.虽然用流程方式实现了,但阅读起来费劲,还容易出错.所以就用了状态机,需要的朋友可以参考下
    2021-06-06
  • 解决ToPILImage时出现维度报错问题pic should be 2/3 dimensional. Got 4 dimensions.

    解决ToPILImage时出现维度报错问题pic should be 2/3 d

    这篇文章主要介绍了解决ToPILImage时出现维度报错问题pic should be 2/3 dimensional. Got 4 dimensions.具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • 基于Python实现签到脚本过程解析

    基于Python实现签到脚本过程解析

    这篇文章主要介绍了基于Python实现签到脚本过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • 用Python制作一个可以聊天的皮卡丘版桌面宠物

    用Python制作一个可以聊天的皮卡丘版桌面宠物

    这篇文章主要为大家介绍如何利用Python制作一个可爱的皮卡丘桌面挂件宠物,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-03-03
  • 一文搞懂Python读取text,CSV,JSON文件的方法

    一文搞懂Python读取text,CSV,JSON文件的方法

    文件处理是一种用于创建文件、写入数据和从中读取数据的过程,Python 拥有丰富的用于处理不同文件类型的包,从而使得我们可以更加轻松方便的完成文件处理的工作,本文将来为大家详细讲讲
    2022-06-06

最新评论