[Tutor] Processing Linux command line output

Alan Gauld alan.gauld at btinternet.com
Thu Apr 25 00:56:32 CEST 2013


On 24/04/13 23:03, Gareth Allen wrote:

> I'm trying to get the output of a command

The subprocess module is the officially endorsed way of doing that 
nowadays. The docs show many use cases depending on what you need.
That will let you captiure the output as a set of strings.

> and split it into a list that I can process.

You can use the string methods to manipulate the output
You probably want the lines separated then use the
strip() and startswith() methods to identify the lines you are 
interested in.

For each line you might use slicing for fixed width fields or
find() to get the starting index of your substrings or maybe
split() to separate by colons.

As a last resort use regex via the re module, but hopefully
you won't need that

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list