python之platform内置模块
前言
①Python 为我们提供了 Platform 模块,该模块用于检索我们正在运行的平台的所有信息。
②os模块中某些功能不是跨平台的(意味着不是始终可用),而platform模块提供了很多跨平台的功能(函数)。
③platform模块是一个 Python 包,可以检索关于我们运行 Python 的平台的所有信息。
相关函数
1、获取计算机操作系统的一些相关信息
import platform import sys # 获取操作系统名称 """ linux """ print(sys.platform) # 获取操作系统名称及版本号 """ Linux-5.15.0-60-generic-x86_64-with-glibc2.31 """ print(platform.platform()) # 获取操作系统版本号 """ #66~20.04.1-Ubuntu SMP Wed Jan 25 09:41:30 UTC 2023 """ print(platform.version()) # 获取操作系统的位数 """ ('64bit', 'ELF') """ print(platform.architecture()) # 获取计算机类型 """ x86_64 """ print(platform.machine()) # 获取计算机的网络名称 """ test-Vostro-3888-China-HDD-Protection """ print(platform.node()) # 获取计算机处理器信息 """ x86_64 """ print(platform.processor()) # 获取操作系统的发布版本 """ 5.15.0-60-generic """ print(platform.release()) # 获取包含上面所有的信息汇总 """ uname_result(system='Linux', node='test-Vostro-3888-China-HDD-Protection', release='5.15.0-60-generic', version='#66~20.04.1-Ubuntu SMP Wed Jan 25 09:41:30 UTC 2023', machine='x86_64') """ print(platform.uname())
2、获取计算机中python的一些相关信息
import platform # The Python build number and date as strings # 返回一个元组,将Python内部版本号和日期表示为字符串。(buildno, builddate)。以字符串形式说明 Python 内部版本号和日期。 print(platform.python_build()) # Returns a string identifying the compiler used for compiling Python # 返回一个字符串,标识用于编译 Python 的编译器。 print(platform.python_compiler()) # Returns a string identifying the Python implementation SCM branch # 返回标识 Python 实现 SCM 分支的字符串。 print(platform.python_branch()) # Returns a string identifying the Python implementation # 返回标识 Python 实现的字符串。 可能的返回值是:“CPython”、“IronPython”、“Jython”、“PyPy”。platform.python_revision()返回标识 Python 实现 SCM 修订的字符串。platform.python_version() print(platform.python_implementation()) # 'Python implementation SCM revision print(platform.python_revision()) # The version of Python # 以字符串形式返回Python版本 print(platform.python_version()) # Python version as tuple # 以字符串的元组 (major, minor, patchlevel) 形式返回 Python 版本。 print(platform.python_version_tuple())
注意: sys.platform 函数返回值各平台统对应值:
column | column |
---|---|
平台 | 值 |
Linux (2.x and 3.x) | 'linux2' |
Windows | 'win32' |
Windows/Cygwin | 'cygwin' |
Mac OS X | 'darwin' |
OS/2 | 'os2' |
OS/2 EMX | 'os2emx' |
RiscOS | 'riscos' |
RiscOS | 'riscos' |
AtheOS | 'atheos' |
示例
import platform 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()
运行结果:
去期待陌生,去拥抱惊喜。
分类:
Python--基础语法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!