output from popen

H

hiral

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.

Thank you in advance.
-Hiral
 
K

Kushal Kumaran

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top