Problems packaging a Java program with a Python subprocess

I have a small java program that I can run from the command line using this syntax:

java -jar EXEV.jar -s:myfile

This Java program prints some data to the screen and I want to redirect stdout

to a file named output.txt

.

from subprocess import Popen, PIPE

def wrapper(*args):
    process = Popen(list(args), stdout=PIPE)
    process.communicate()[0]
    return process

x = wrapper('java', '-jar', 'EXEV.jar', '-s:myfile', '>', 'output.txt')

      

When I run the above, it output.txt

never gets written and Python throws no errors. Can anyone help me figure out the problem?

+3


source to share


1 answer


You need to either use stdout=output

, where output

is the open file to write to 'output.txt' and remove the output redirection from the command, or leave the output redirection in the command and use shell=True

with an argument stdout

:

Option 1:

from subprocess import Popen

def wrapper(*args):
    output = open('output.txt', w)
    process = Popen(list(args), stdout=output)
    process.communicate()
    output.close()
    return process

x = wrapper('java', '-jar', 'EXEV.jar', '-s:myfile')

      



Option 2:

from subprocess import Popen

def wrapper(*args):
    process = Popen(' '.join(args), shell=True)
    process.communicate()
    return process

x = wrapper('java', '-jar', 'EXEV.jar', '-s:myfile', '>', 'output.txt')

      

+3


source







All Articles