raton roguille
raton roguille

Reputation: 191

redirect output of jar file in python

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

Upvotes: 0

Views: 504

Answers (1)

Avihoo Mamka
Avihoo Mamka

Reputation: 4786

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)

Upvotes: 1

Related Questions