how to get output from system call into data object

Thomas Wouters thomas at
Fri Aug 27 11:57:14 EDT 1999

On Fri, Aug 27, 1999 at 12:24:08PM -0500, Stephen M. Shelly wrote:

> I know this is not what I should be doing, but I do not have a 
> good enough understanding of python to figure out how to create 
> my list of data objects 
> from this output.
> is it like:
> MyList = system("Command | awk {print what i need}") ??????????

Well, it depends on what kind of list you want. Does the awk print one line
for each row in your list, and are columns seperated by spaces ? Then this
should work:

import os, string

endlist = []
program = os.popen("command | awk { print what i need}", "r"):

for line in program.readlines():

You need to use os.popen instead of os.system, as you want to capture the
output. os.popen returns a file-like object that you can use to read the
lines. Note that the 'readlines()' reads all lines at once, and so doesn't
return until the command you opened with popen() is finished. If the command
| awk {etc} is going to take a long time, and you want to see that it's
working, you may want to do something like

while 1:
	line = program.readline()
	if not line:
	print string.join(endlist[-1])

instead of the for loop above.

If your data isn't seperated by whitespace but by some other character, you
can give this as the second character to split:

>>> string.split("a:b:c:d:e:f",":")
['a', 'b', 'c', 'd', 'e', 'f']

Or if the data is fixed-length, you can use slicing instead:

endlist.append( [ line[:5],line[5:10], line[10:] ] )

See the tutorial ( for more info on these
topics, plus a LOT more examples. (and better ones ;-)

Thomas Wouters <thomas at>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

More information about the Python-list mailing list