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


Posted in Python onOctober 09, 2020

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 相关文章推荐
python文件比较示例分享
Jan 10 Python
python的三目运算符和not in运算符使用示例
Mar 03 Python
Python文件及目录操作实例详解
Jun 04 Python
浅谈pyhton学习中出现的各种问题(新手必看)
May 17 Python
Python 多进程并发操作中进程池Pool的实例
Nov 01 Python
Python中pow()和math.pow()函数用法示例
Feb 11 Python
解决python中使用plot画图,图不显示的问题
Jul 04 Python
详解Python最长公共子串和最长公共子序列的实现
Jul 07 Python
Python爬虫常用库的安装及其环境配置
Sep 19 Python
Python2和Python3的共存和切换使用
Apr 12 Python
python爬虫 基于requests模块发起ajax的get请求实现解析
Aug 20 Python
浅析Python OpenCV三种滤镜效果
Apr 11 Python
python smtplib发送多个email联系人的实现
Oct 09 #Python
python 决策树算法的实现
Oct 09 #Python
Python+unittest+requests 接口自动化测试框架搭建教程
Oct 09 #Python
Python实现http接口自动化测试的示例代码
Oct 09 #Python
python两种注释用法的示例
Oct 09 #Python
Python实现扫码工具的示例代码
Oct 09 #Python
如何完美的建立一个python项目
Oct 09 #Python
You might like
PHP实现的封装验证码类详解
2013/06/18 PHP
PHP随机数 C扩展随机数
2016/05/04 PHP
php实现背景图上添加圆形logo图标的方法
2016/11/17 PHP
php获取ip及网址的简单方法(必看)
2017/04/01 PHP
Yii框架页面渲染操作实例详解
2019/07/19 PHP
从零开始学习jQuery (六) jquery中的AJAX使用
2011/02/23 Javascript
Javascript判断对象是否相等实现代码
2013/03/18 Javascript
javascript判断chrome浏览器的方法
2014/03/26 Javascript
js调试系列 源码定位与调试[基础篇]
2014/06/18 Javascript
JavaScript 里的类数组对象
2015/04/08 Javascript
js实现刷新iframe的方法汇总
2015/04/27 Javascript
javascript使用shift+click实现选择和反选checkbox的方法
2015/05/04 Javascript
javascript实现状态栏文字首尾相接循环滚动的方法
2015/07/22 Javascript
浅析Ajax语法
2016/12/05 Javascript
详解axios在vue中的简单配置与使用
2017/05/10 Javascript
微信小程序使用Socket的实例
2017/09/19 Javascript
js+html获取系统当前时间
2017/11/10 Javascript
jQuery实现的监听导航滚动置顶状态功能示例
2018/07/23 jQuery
解决vue 中 echart 在子组件中只显示一次的问题
2018/08/07 Javascript
Koa日志中间件封装开发详解
2019/03/09 Javascript
vue实现直播间点赞飘心效果的示例代码
2019/09/20 Javascript
解决vue自定义指令导致的内存泄漏问题
2020/08/04 Javascript
Vue如何跨组件传递Slot的实现
2020/12/14 Vue.js
spark dataframe 将一列展开,把该列所有值都变成新列的方法
2019/01/29 Python
Python爬虫——爬取豆瓣电影Top250代码实例
2019/04/17 Python
Django项目使用CircleCI的方法示例
2019/07/14 Python
Matplotlib scatter绘制散点图的方法实现
2020/01/02 Python
python的列表List求均值和中位数实例
2020/03/03 Python
Python3实现打印任意宽度的菱形代码
2020/04/12 Python
css3弹性盒子flex实现三栏布局的实现
2020/11/12 HTML / CSS
英国的知名精品百货公司:House of Fraser(福来德)
2016/08/14 全球购物
努比亚手机官网:nubia
2016/10/06 全球购物
总裁秘书岗位职责
2013/12/04 职场文书
行政部经理助理岗位职责
2014/06/15 职场文书
小区门卫岗位职责范本
2014/08/24 职场文书
古诗之感恩老师
2019/10/24 职场文书