You are not logged in.

#1 2006-09-25 10:08:19

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Pull cpu speed from cpuinfo in Python?

Hi,

I am trying to get cpu Mhz from cat /proc/cpuinfo...

#!/usr/bin/env python

# Pull Cpu speed from /proc/cpuinfo

import os
import sys
import string

def lin_com(b):
    h = os.popen(b)
    k = h.readlines()
    #k = k[:len(k)-1]
    h.close()
    return k


g =  lin_com("cat /proc/cpuinfo")
g = g[6]
g = g.split(':')[1:]

print g

but I get ['<speed>'/n] wanted to clean up output but not sure how ....

I know cpu speed is in line 6 of array or would it be better to search for string in cpuinfo?

Emm not sure kinda in the dark here .....?


Mr Green

Offline

#2 2006-09-25 12:48:12

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Pull cpu speed from cpuinfo in Python?

for your output issue:

g = g.split(':')[1].strip()

I would search for the keywords though instead of using a set line number.

cpu_mhz = ''
for line in k:
   if 'MHz' in line:
           cpu_mhz = line.split(bla bla)

I would also just open the file instead of using a pipe with cat.

f = os.open('/proc/cpuinfo', 'r')
f.readlines()
f.close()

Offline

#3 2006-09-25 12:53:38

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,694
Website

Re: Pull cpu speed from cpuinfo in Python?

Or you could use grep like in:

cat /proc/cpuinfo | grep 'model name'

Offline

#4 2006-09-25 13:00:45

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Pull cpu speed from cpuinfo in Python?

cpu_mhz = line.split(bla bla)

I cannot find bla bla in Py docs  :?  j/k

Yeah I opening file is neater ... I see if I can clean it up a bit. ..

cat /proc/cpuinfo | grep 'cpu Mhz' gives not output here maybe the tabs spaces I don't know .....

 f = os.open('/proc/cpuinfo', 'r')
TypeError: an integer is required

very confused.....


Mr Green

Offline

#5 2006-09-25 13:37:40

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Pull cpu speed from cpuinfo in Python?

sorry, its open()

Offline

#6 2006-09-25 13:41:39

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Pull cpu speed from cpuinfo in Python?

no no dude I got it lol...

/me opens Learn Python in 10 seconds

I get

['', ' MHztt: 3220.212n']

emmm need to pull out digits re maybe?


Mr Green

Offline

#7 2006-09-25 14:17:24

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Pull cpu speed from cpuinfo in Python?

>>> for line in lines:
...     if 'MHz' in line:
...             cpu_MHz = line.split(':')[1].strip()
... 
>>> cpu_MHz
'2071.203'

Offline

#8 2006-09-25 14:20:16

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,893
Website

Re: Pull cpu speed from cpuinfo in Python?

no waay dude ... Wow!!!!  8)

Now can you wrap it in gtk for me please rofl


Mr Green

Offline

Board footer

Powered by FluxBB