linux系統(tǒng)使用python獲取cpu信息腳本分享
linux系統(tǒng)使用python獲取cpu信息腳本分享
#!/usr/bin/env Python
from __future__ import print_function
from collections import OrderedDict
import pprint
def CPUinfo():
''' Return the information in /proc/CPUinfo
as a dictionary in the following format:
CPU_info['proc0']={...}
CPU_info['proc1']={...}
'''
CPUinfo=OrderedDict()
procinfo=OrderedDict()
nprocs = 0
with open('/proc/CPUinfo') as f:
for line in f:
if not line.strip():
# end of one processor
CPUinfo['proc%s' % nprocs] = procinfo
nprocs=nprocs+1
# Reset
procinfo=OrderedDict()
else:
if len(line.split(':')) == 2:
procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip()
else:
procinfo[line.split(':')[0].strip()] = ''
return CPUinfo
if __name__=='__main__':
CPUinfo = CPUinfo()
for processor in CPUinfo.keys():
print(CPUinfo[processor]['model name'])
簡單說明一下清單 1,讀取/proc/CPUinfo 中的信息,返回 list,每核心一個 dict。其中 list 是一個使用方括號括起來的有序元素集合。List 可以作為以 0 下標開始的數組。Dict 是 Python 的內置數據類型之一, 它定義了鍵和值之間一對一的關系。OrderedDict 是一個字典子類,可以記住其內容增加的順序。常規(guī) dict 并不跟蹤插入順序,迭代處理時會根據鍵在散列表中存儲的順序來生成值。在 OrderedDict 中則相反,它會記住元素插入的順序,并在創(chuàng)建迭代器時使用這個順序。
可以使用 Python 命令運行腳本 CPU1.py 結果見圖
- python獲取linux系統(tǒng)信息的三種方法
- Bash 腳本實現每次登錄到 Shell 時可以查看 Linux 系統(tǒng)信息
- 使用 Python 獲取 Linux 系統(tǒng)信息的代碼
- Linux系統(tǒng)信息查看常用命令
- Linux、ubuntu系統(tǒng)下查看顯卡型號、顯卡信息詳解
- Linux系統(tǒng)查看CPU、機器型號、內存等信息
- 使用Python獲取Linux系統(tǒng)的各種信息
- 詳解linux下查看系統(tǒng)版本號信息的方法(總結)
- 使用python獲取CPU和內存信息的思路與實現(linux系統(tǒng))
- linux系統(tǒng)獲取硬盤使用信息
- Linux系統(tǒng)下利用C程序輸出某進程的內存占用信息
- linux如何查看系統(tǒng)信息