output from popen

Kushal Kumaran kushal.kumaran+python at gmail.com
Mon Apr 5 04:31:17 EDT 2010


On Mon, Apr 5, 2010 at 1:33 PM, hiral <hiralsmaillist at gmail.com> wrote:
> Hi,
> I am trying following script...
>
> <script>
> import subprocess
> cmd=['time', 'myCmd']
> p = subprocess.Popen(cmd)
> p.communicate()
> <script>
> Where 'myCmd' is some executable path and combination of arguments.
>
> Now I am observing following output...
> <myCmd_output...>
> 0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata
> 0maxresident)k
> 0inputs+0outputs (167major+23minor)pagefaults 0swaps
>
> As you can observe output of 'time' command got printed as garbage.
> Can you please let me know how can I get output of 'time' same as
> terminal.
>

First, the output of "time" is not actually garbage.  It probably has
all the information you need.  Second, when you run "time" from the
terminal, you are probably invoking the version of "time" built in to
your shell.  You can try explicitly running /usr/bin/time (probably,
could be /bin/time or something else) to get the output you're seeing
from your program.

Passing shell=True to subprocess.Popen invokes a shell to run your
command.  You will also need to pass executable="path-to-your-shell"
for exact results.  That should give you the same output as you get
from a terminal.  You will probably need to change the "cmd" arg to be
a string.

-- 
regards,
kushal



More information about the Python-list mailing list