I always got this question in my mind. How to run a command line within python, get the output and manipulates it. Before I learn python, I was doing bash scripts all the while to helps me manipulates text which I get it from log files, or pipes out from some certain command line.
To do it in bash script is straight forward, where I just puts my command line into text like I put in my bash prompt. But to run a command line in python is not like bash script, so what options do I have?
The first function I discover was system in os library.
import os os.system("ls -l")
By passing the command line to the system function will cause the command executed in subshell and print it straight away to standard output. I don’t have chance to keep the outcomes for further manipulations.
The second function that drags my attention is popen, which the same way I pass my command line in, and this time it returns the result into a file object which allows me to manipulate it consequently.
import os f=os.popen("ls -l") for i in f.readlines(): print "myresult:",i,
What it returns for me is a the result of standard output, what if I purposely make a wrong command line? let say …
import os f=os.popen("ls -z")
The error will be print to screen instead of store in file object f,
ls: invalid option -- z Try `ls --help' for more information.
The error is actually print to standard error, to store the standard error, we need to call popen3().
fi,fo,fe=os.popen3("ls -z") for i in fe.readlines(): print "error:",i
popen3() returns stdin, stdout and stderr which are standard input, standard output and the standard error. The examples above, I extract the error description from the stderr file object.
How can I tell the command line is success or fail? I have no idea too, the only thing I can do is run readlines() to extract the output from respective file object.
There is another Popen function in subprocess library, I will covers it after I get my hands dirty on that lib more.