Grep Equivalent for Python

tereglow tom.rectenwald at eglow.net
Wed Mar 14 18:55:33 CET 2007


Okay,

It is now working as follows:

memFile = open('/proc/meminfo')
for line in memFile.readlines():
	if line.startswith("MemTotal"):
		memStr = line.split()
		memTotal = memStr[1]
memFile.close()
print "Memory: " + memTotal + "kB"

I'm learning the whole try, finally exception stuff so will add that
in as well.  Now, I'm trying to figure out the CPU speed.  In shell,
I'd do:

grep "^cpu MHz" /proc/cpuinfo | awk '{print $4}' | head -1

The "head -1" is added because if the server has 2 or more processors,
2 or more lines will result, and I only need data from the first
line.  So, now I'm looking for the equivalent to "head (or tail" in
Python.  Is this a case where I'll need to break down and use the re
module?  No need to give me the answer, a hint in the right direction
would be great though.

Thanks again,
Tom




More information about the Python-list mailing list