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进阶教程之模块(module)介绍
Aug 30 Python
python通过openpyxl生成Excel文件的方法
May 12 Python
Windows系统下多版本pip的共存问题详解
Oct 10 Python
Python学习之Anaconda的使用与配置方法
Jan 04 Python
Python爬虫包BeautifulSoup简介与安装(一)
Jun 17 Python
python 如何去除字符串头尾的多余符号
Nov 19 Python
python orm 框架中sqlalchemy用法实例详解
Feb 02 Python
完美解决pycharm导入自己写的py文件爆红问题
Feb 12 Python
python matplotlib.pyplot.plot()参数用法
Apr 14 Python
win10下python3.8的PIL库安装过程
Jun 08 Python
Python类成员继承重写的实现
Sep 16 Python
Python读写Excel表格的方法
Mar 02 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中使用cURL实现Get和Post请求的方法
2013/03/13 PHP
IIS6.0 开启Gzip方法及PHP Gzip函数分享
2014/06/08 PHP
php实现在线考试系统【附源码】
2018/09/18 PHP
javascript 面向对象编程 聊聊对象的事
2009/09/17 Javascript
修改js Calendar日历控件 兼容IE9/谷歌/火狐
2013/01/04 Javascript
asp.net中System.Timers.Timer的使用方法
2013/03/20 Javascript
深入Javascript函数、递归与闭包(执行环境、变量对象与作用域链)使用详解
2013/05/08 Javascript
node.js中的fs.utimesSync方法使用说明
2014/12/15 Javascript
javascript设计简单的秒表计时器
2020/09/05 Javascript
jQuery获取file控件中图片的宽高与大小
2016/08/04 Javascript
基于vue2实现上拉加载功能
2017/11/28 Javascript
jquery 实现拖动文件上传加载进度条功能
2018/03/18 jQuery
[原创]微信小程序获取网络类型的方法示例
2019/03/01 Javascript
vue动态子组件的两种实现方式
2019/09/01 Javascript
layer.open 子页面弹出层向父页面传输数据的例子
2019/09/26 Javascript
jQuery实时统计输入框字数及限制
2020/06/24 jQuery
[48:18]DOTA2-DPC中国联赛 正赛 RNG vs Dynasty BO3 第二场 1月29日
2021/03/11 DOTA
Python读取网页内容的方法
2015/07/30 Python
Python使用multiprocessing实现一个最简单的分布式作业调度系统
2016/03/14 Python
Django中间件工作流程及写法实例代码
2018/02/06 Python
使用pandas模块读取csv文件和excel表格,并用matplotlib画图的方法
2018/06/22 Python
python将回车作为输入内容的实例
2018/06/23 Python
python之super的使用小结
2018/08/13 Python
Linux下python3.6.1环境配置教程
2018/09/26 Python
用python标准库difflib比较两份文件的异同详解
2018/11/16 Python
python 反编译exe文件为py文件的实例代码
2019/06/27 Python
使用keras实现densenet和Xception的模型融合
2020/05/23 Python
Django web自定义通用权限控制实现方法
2020/11/24 Python
制药工程专业毕业生推荐信
2013/12/24 职场文书
中学生期末评语
2014/02/03 职场文书
2014年五四青年节活动策划书
2014/04/22 职场文书
献爱心大型公益活动策划方案
2014/09/15 职场文书
法人委托书范本格式
2014/09/15 职场文书
教师自我剖析材料范文
2014/09/30 职场文书
保护环境建议书作文500字
2015/09/14 职场文书
MySQL 表空间碎片的概念及相关问题解决
2021/05/07 MySQL