Linux command-line call not returning what it should from os.system? Linux command-line call not returning what it should from os.system? python python

Linux command-line call not returning what it should from os.system?


What gets returned is the return value of executing this command. What you see in while executing it directly is the output of the command in stdout. That 0 is returned means, there was no error in execution.

Use popen etc for capturing the output .

Some thing along this line:

import subprocess as subp = sub.Popen(['your command', 'arg1', 'arg2', ...],stdout=sub.PIPE,stderr=sub.PIPE)output, errors = p.communicate()print output

or

import osp = os.popen('command',"r")while 1:    line = p.readline()    if not line: break    print line

ON SO : Popen and python


If you're only interested in the output from the process, it's easiest to use subprocess' check_output function:

output = subprocess.check_output(["command", "arg1", "arg2"]);

Then output holds the program output to stdout. Check the link above for more info.


The simplest way is like this:

import osretvalue = os.popen("ps -p 2993 -o time --no-headers").readlines()print retvalue

This will be returned as a list