Pipe jar output to file in python

Adam_G :

I am running a jar file from python using something to this

import subprocess
subprocess.call(['java', '-jar', 'tool.jar'])

I would then like to save the output to a file, but the issue is that the original java code has the output being sent to System.err.println(output). I would love to modify the original java file, but this is not an option.

How can I send this output to a file? I have tried:

subprocess.call(['java', '-jar', 'chattool.jar', '>', 'out.txt', '2','>','err.txt'])

as well as

subprocess.call(['java', '-jar', 'chattool.jar', '&>', 'test.txt'])

but neither one redirects the output. Is it possible to do this within python?

Maurice Meyer :

You can pipe stdout/stderr into a file using Popen():

_stdout = open('/tmp/chattool.stdout.txt', 'w')
_stderr = open('/tmp/chattool.stderr.txt', 'w')

cmd = subprocess.Popen('java -jar chattool.jar', stderr=_stderr, stdout=_stdout, cwd=_cwd, shell=True)
cmd.communicate() 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358901&siteId=1