首页 > 编程语言 >Python platform模块获取操作系统信息

Python platform模块获取操作系统信息

时间:2023-04-05 18:32:23浏览次数:28  
标签:操作系统 get Python python platform version print def

一、概述

1、python中,platform模块给我们提供了很多方法去获取操作系统的信息
import platform
print(platform.platform())# 获取操作系统名称和版本号:macOS-10.14.6-x86_64-i386-64bit
print(platform.system())# 获取操作系统:Darwin
print(platform.version()) # 获取计算机操作系统版本号:arwin Kernel Version 18.7.0: Mon Mar  8 22:11:48 PST 2021; root:xnu-1903.288.66~1/RELEASE_X86_64
print(platform.release()) #获取计算机操作系统的版本 18.7.0
print(platform.architecture()) #获取操作系统的位数 ('64bit', '')
print(platform.node())  # 计算机网络名称  yanzis-MacBook-Pro.local
print(platform.machine())  #计算机类型 x86_64
print(platform.processor()) #计算机处理器信息  i386
print(platform.uname())#获取以上信息的综合:uname_result(system='Darwin', node='yanzis-MacBook-Pro.local', release='18.7.0', version='Darwin Kernel Version 18.7.0: Mon Mar  8 22:11:48 PST 2021; root:xnu-1903.288.66~1/RELEASE_X86_64', machine='x86_64')

2、获得计算机中一些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_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 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 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()

标签:操作系统,get,Python,python,platform,version,print,def
From: https://blog.51cto.com/u_13236892/6171292

相关文章

  • python列表的添加的四种方式
    列表删除的五种方式python列表的增删改1、list增加元素1.1append()1.2extend()1.3insert()1.4切片1、list增加元素python中列表增加元素有四种方式:append():在列表末尾添加一个元素extend():在列表末尾添加至少一个元素insert():在列表任意位置添加一个元素切片:在列表任意位......
  • python中列表的删除操作,五种方式
    列表删除操作1、列表删除操作1.1remove()1.2pop()1.3切片1.4clear与del 1、列表删除操作五种方式分别为:remove():一次删除一个元素;如果列表内有重复元素则删除第一个;元素不存在时抛出异常ValueErrorpop():删除一个指定的索引位置上的元素;指定索引不存在则......
  • python split()截取一部分的字符串及按照指定字符或者长度 截取字符串
     str='https://www.baidu.com/pdf/abcdefg.pdf'#输出字符串,>>>https://www.baidu.com/pdf/abcdefg.pdfprint(str)#做为一个整体截取,>>>['https://www.baidu.com/pdf/abcdefg.pdf']print(str.split())#把字符串分割,>>>[&......
  • python list tuple dict set
    pythonlist列表tuple元组dict字典set集合Python语言简洁明了,可以用较少的代码实现同样的功能。这其中Python的四个内置数据类型功不可没,他们即是list,tuple,dict,set。这里对他们进行一个简明的总结。https://www.cnblogs.com/soaringeveryday/p/5044007.htmltuple是一个不......
  • 一个神奇的需求:doc批量转docx,1行Python代码实现
    大家好,这里是程序员晚枫,今天给大家分享一个Python自动化办公的知识:1行代码,批量给把doc文档转为docx格式。1、上代码下载Python自动化办公的专用库:python-office,下载命令如下。pipinstallpython-office-ihttps://pypi.python.org/simple-U注意,最近清华镜像和阿里镜像都不......
  • python 报错AssertionError: process has already started
    python报错AssertionError:processhasalreadystarted现象  原因在Python中设置守护进程daemon,一定要放在start方法上面才会有效解决方法 ......
  • 使用python读取指定目录下的指定类型文件
    准备工作:设置指定的路径,使用os.listdir()方法获取路径下所有的文件importospath="d:\\data"#设置路径dirs=os.listdir(path)#获取指定路径下的文件循环判断:使用os.path.splitext()方法筛选出指定类型的文件foriin......
  • 01:python基础
      正文#打印内容print()输入内容input()print("helloWorld!")#1:注释:输入内容#name=input("请输入你的名字:")#print("hello,",name,"您好")print("1024*768=",1024*768)#2:4个缩进代表代码块#Python程序是大小写敏感的,如果写错了大小写,程序会......
  • python flask 框架后端如何获取前端的表单数据 文本 单选框 多选框
    文本pyhon后端用request.values.get("name")去获取  if request.method == "POST":        username = request.values.get("username")     sex = request.values.get("sex") 二、多选按钮checkbox  <......
  • 一些常用的Python调试工具
    pdb:Python自带的调试工具,可以在代码中添加断点并逐步执行代码,以便逐步检查代码执行的过程。ipdb:基于pdb的增强版,支持更多的交互式调试功能。PyCharm:一个常用的Python集成开发环境,可以通过图形化界面来调试程序。VisualStudioCode:另一个常用的Python集成开发环境,也可......