python获取系统信息模块详解
python是跨平台语言,有时候我们的程序需要运行在不同系统上,例如:linux、MacOs、 Windows,为了使程序有更好通用性,需要根据不同系统使用不同操作方式。我们可以使用platform模块来获取系统信息。
platform是python自带模块,我们可以直接使用,下面来介绍这个模块:
首先导入模块:import platform,常用方法如下:
下面我们实际看下在window下获取信息:
def showinfo(tip, info): print("{}:{}".format(tip,info))showinfo("操作系统及版本信息",platform.platform())showinfo('获取系统版本号',platform.version())showinfo('获取系统名称', platform.system())showinfo('系统位数', platform.architecture())showinfo('计算机类型', platform.machine())showinfo('计算机名称', platform.node())showinfo('处理器类型', platform.processor())showinfo('计算机相关信息', platform.uname())
输出结果如下:
操作系统及版本信息:Windows-10-10.0.17134-SP0获取系统版本号:10.0.17134获取系统名称:Windows系统位数:('64bit', 'WindowsPE')计算机类型:AMD64计算机名称:DESKTOP-83IAUFP处理器类型:Intel64 Family 6 Model 142 Stepping 10, GenuineIntel计算机相关信息:uname_result(system='Windows', node='DESKTOP-83IAUFP', release='10', version='10.0.17134', machine='AMD64', processor='Intel64 Family 6 Model 142 Stepping 10, GenuineIntel')
我们再换Ubuntu执行,输出结果如下:
操作系统及版本信息:Linux-4.13.0-39-generic-x86_64-with-debian-stretch-sid获取系统版本号:#44~16.04.1-Ubuntu SMP Thu Apr 5 16:43:10 UTC 2018获取系统名称:Linux系统位数:('64bit', '')计算机类型:x86_64计算机名称:ubuntu处理器类型:x86_64计算机相关信息:uname_result(system='Linux', node='ubuntu', release='4.13.0-39-generic', version='#44~16.04.1-Ubuntu SMP Thu Apr 5 16:43:10 UTC 2018', machine='x86_64', processor='x86_64')
我们可以根据platform.system()获取系统类型。
下面我们使用platform获取Python版本相关信息,主要方法如下:方法 说明 platform.python_build()Python编译信息platform.python_version()获取Python版本信息
我们来看下当前使用Python相关信息:
def showinfo(tip, info): print("{}:{}".format(tip,info))showinfo('编译信息:', platform.python_build())showinfo('版本信息:', platform.python_version())
输出结果:
编译信息::('default', 'Oct 28 2018 19:44:12')版本信息::3.6.7
如果我们想要获取系统相关信息直接可以使用platform模块。
更多Python知识可以关注老猫专栏:
https://blog.51cto.com/cloumn/detail/34 。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。