0

I want to redirect the output of my jar in a file with python I have tried the following but it didn't work out

import sys
import subprocess

cmdargs = sys.argv
fname = str(cmdargs[1])
input = '../res/test/'+fname
output = '../res/res/'+fname
subprocess.Popen(['java', '-jar', '../res/chemTagger2.jar',input,'>',output]) 

the output is print in the console

raton roguille
  • 181
  • 2
  • 11
  • Possible duplicate of [Write to file descriptor 3 of a Python subprocess.Popen object](http://stackoverflow.com/questions/6050187/write-to-file-descriptor-3-of-a-python-subprocess-popen-object) See also http://stackoverflow.com/questions/18421757/live-output-from-subprocess-command – fredtantini Dec 09 '16 at 11:41

1 Answers1

1

You can redirect the subprocess.Popen stdout and stderr by using their parameters within the Popen command, as follows:

import sys
import subprocess

cmdargs = sys.argv
fname = str(cmdargs[1])
input = '../res/test/' + fname
output = '../res/res/' + fname

with open(output, 'a') as f_output:
    subprocess.Popen(['java', '-jar', '../res/chemTagger2.jar',input], stdout=f_output)
Avihoo Mamka
  • 4,656
  • 3
  • 31
  • 44