0

My python script calls an executable (binary compiled from C++) like this:

subprocess.call(["./rti", '1', '0.05', fileForRTI])

where rti executable name, 1, 0.05 and fileForRTI are arguments.

This executable generates output to the console, but I want to output it to the text file as well. What I tried is this (according to this):

import sys

someFile = 'testFile'
sys.stdout = open(someFile, "w")
print(str(subprocess.call(["./rti", '1', '0.05', fileForRTI])))

But for some reason what is written to the file is just '1', while output is much bigger.

I also tried this:

p = Popen(['./rti', '1', '0.05', fileForRTI], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
rc = p.returncode
print(rc)

But it's once again shows 1.

EDIT: also Kasra's solution give output 1 instead of what I see normally on the screen.

Not sure if allowed, but a side question, if that ./rti executable takes long time to generate code, so is it possible to stop terminate and output to file what is already seen on the screen.

Community
  • 1
  • 1
estranged
  • 424
  • 5
  • 16

1 Answers1

1

You can use subprocess.check_output() :

import subprocess
try:
    out_bytes = subprocess.check_output(["./rti", '1', '0.05', fileForRTI])
except subprocess.CalledProcessError as e:
    out_bytes = e.output # Output generated before error
    code= e.returncode # Return code

This runs the specified command and returns its output as a byte string. If you need to interpret the resulting bytes as text, add a further decoding step. For example:

out_text = out_bytes.decode('utf-8')

The check_output() function is the easiest way to execute an external command and get its output. However, if you need to perform more advanced communication with a subprocess, such as sending it input, you’ll need to take a difference approach. For that,use the subprocess.Popen class directly

Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • Well I get an error with this: `raise CalledProcessError(retcode, cmd, output=output) subprocess.CalledProcessError: Command '['./rti', '1', '0.05', 'autom1Z.data']' returned non-zero exit status 1` – estranged Dec 14 '14 at 17:01
  • That's a feature of `check_output()`. Use one of the other related functions from `subprocess` if you need or want to ignore the exit status. – tripleee Dec 14 '14 at 17:03
  • @estranged You can use an exception handeling , i edit the answer ! – Mazdak Dec 14 '14 at 17:04
  • Gotcha! It works but I once again get '1' as an output. Everytime I simply execute program I get normal output to console, but if I output using your method, or ones in my question, I get 1 instead and I cannot see any normal output. – estranged Dec 14 '14 at 17:08
  • @Kasra it's compiled C++ binary, which I can run in linux terminal like this: `./rti 1 0.05 fileName`. It sits in same directory as my .py python program. I need to run it from python script, and so far it works, but only I see output to the screen. – estranged Dec 14 '14 at 17:14
  • can you change `stdout =PIPE,stdin =PIPE` with `stdout = subprocess.PIPE, stdin = subprocess.PIPE` ? – Mazdak Dec 14 '14 at 17:19
  • Actually it works, I just was outputting error code instead of output, just like someone in comment said. Thanks sir. Now on a related note, if the executable takes very long to finish, is it possible to terminate it after let's say 5 seconds? – estranged Dec 14 '14 at 17:24
  • @estranged you're welcome you can use `signal` package , read more here http://stackoverflow.com/questions/492519/timeout-on-a-python-function-call – Mazdak Dec 14 '14 at 17:46