'mandriva', 'rocks', 'slackware', 'yellowdog', 'gentoo',
'UnitedLinux', 'turbolinux')
如果你的linux发布版本不在其中(或者其中之一的衍生发行版)。那么你很可能调用了上面这个函数而看不到任何有用的信息。
platform模块的最后一个函数,我们将会看看architecture()函数。当你无参的调用这个函数,它会返回包含架构位数以及python可执行的格式的元组,像这样:
>>> platform.architecture()
('64bit', 'ELF')
在32位的系统上,你将会看到:
>>> platform.architecture()
('32bit', 'ELF')
如果你指定了系统上其他任何可执行的,你都将会获得相似的结果,如同这样:
>>> platform.architecture(executable='/usr/bin/ls')
('64bit', 'ELF')
鼓励探索platform模块的除了这些的其它函数,找出你现在运行的Python版本。如果你想知道这个模块是如何获取这些信息的,你可以深入查看PYthon源码目录下的Lib/platform.py文件。
os和sys模块也可以获得一些系统属性,例如原生的字节序。接下来,我们超越Python标准库模块,去探索一些在linux系统通过proc和sysfs文件系统使之访问信息成为可能。注意的是通过文件系统来访问信息将会在不同的硬件架构有所不同。所以在读本文或是写脚本时要时刻记住可以试图从这些文件获取信息。
获取CPU信息
/proc/cpuinfo文件包含了你的系统处理器单元的信息。例如,这里就是python版的linux命令cat /proc/cpuinfo所做的事:
#! /usr/bin/env python
""" print out the /proc/cpuinfo
file
"""
from __future__ import print_function
with open('/proc/cpuinfo') as f:
for line in f:
print(line.rstrip('n'))
当你使用Python 2 或者 Python 3执行这个程序时,你会在屏幕上看到所有/proc/cpuinfo的内容(在上面的程序里,rstrip()方法用来删除每行末尾的换行符)
在下面的代码里列举了使用startwith()字符串方法来显示你的处理器单元的模式。
#! /usr/bin/env python
""" Print the model of your
processing units
"""
from __future__ import print_function
with open('/proc/cpuinfo') as f:
for line in f:
# Ignore the blank line separating the information between
# details about two processing units
if line.strip():
if line.rstrip('n').startswith('model name'):
model_name = line.rstrip('n').split(':')[1]










