0

Need some help with following code - I want to pass string elements of three list as input to java program that I am calling in my python script. Here is what I have done so far -

import subprocess
from subprocess import PIPE
from subprocess import Popen

amount = ['23.5', '12.5', '56.0', '176.1']
invoice_no = ['11290', '12892', '12802', '23489']
date = ['2/3/19', '12/30/17', '04/21/2018', '8/12/17', '12/21/18']

## problem is proc.communicate(). I am currently passing only invoice_no[i] as input.

for i in range (0, len(invoice_no)):
    proc = subprocess.Popen(['java', '-jar', '/data/python/xyz.jar'],stdin=PIPE,stdout=PIPE, stderr=STDOUT)
    q = proc.communicate(input = invoice_no[i])
    print(q)
## But I want to pass amount and date as input as well. 
## I dont know the syntax for that. I also want output as 1 or 0 i.e success or failure. Does anyone know syntax for this? 

Most of the questions which I have seen are passing single string as input.But that's not what I am looking for. Even official document of subprocess is not helpful in finding out how to pass multiple inputs syntax. Links are - running java program from python script

How to execute java program using python considering inputs and outputs both

2
  • 1
    Shouldn't -jar argument should also be a separate parameter to Popen? Commented Jan 22, 2019 at 12:35
  • Not exactly sure.. I am new to python. I was able to reach this after going through many questions and documents. Commented Jan 22, 2019 at 13:09

1 Answer 1

0

Since I have found answer. I thought I will post it here. May be someone will find it useful -

   import subprocess
   from subprocess import PIPE
   from subprocess import Popen

   amount = ['23.5', '12.5', '56.0', '176.1']
   invoice_no = ['11290', '12892', '12802', '23489']
   date = ['2/3/19', '12/30/17', '04/21/2018', '8/12/17', '12/21/18']
   for i in range (0, len(invoice_no)):
       proc = subprocess.Popen(['java', '-jar', '/data/python/xyz.jar', invoice_no[i], date[i], amount[i]],stdin=PIPE,stdout=PIPE, stderr=PIPE)
       output = proc.communicate()[0] ## this will capture the output of script called in the parent script.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.