I am new to python subprocess and i do not understand the documentation.
I am trying to run a jar file within my python program and pipe the output back to python.
sys.stdout.write("haha")
subprocess.Popen(['java', 'myjarfile.jar'], stdin= subprocess.PIPE,
stdout = subprocess.PIPE)
ans = raw_input("")
However, this does not seem to work
Terry Jan Reedy
19.3k3 gold badges44 silver badges57 bronze badges
asked Mar 2, 2015 at 3:40
aceminer
4,36512 gold badges63 silver badges110 bronze badges
1 Answer 1
To pass b'haha' bytestring as an input to the java child process and to get its stdout as ans bytestring:
#!/usr/bin/env python3
from subprocess import check_output
ans = check_output(['java', '-jar', 'myjarfile.jar'], input=b'haha')
input parameter is supported only since Python 3.4, you could use .communicate() on older Python versions:
#!/usr/bin/env python
from subprocess import Popen, PIPE
p = Popen(['java', '-jar', 'myjarfile.jar'], stdin=PIPE, stdout=PIPE)
ans = p.communicate(b'haha')[0]
if p.returncode != 0:
raise Error
answered Mar 10, 2015 at 16:28
jfs
417k211 gold badges1k silver badges1.7k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
sys.stdout.write()andraw_input()to achieve in this case?