1

I can successfully hard code my values as follows below: (my app launches and the parameters are passed and it runs as if I hit enter after manually entering in each parameter as I would if I ran it via command line, inputting the parameters manually and hitting enter after each one):

from subprocess import Popen, PIPE, STDOUT
p = Popen(['C:\\Program Files\\app\\Bin\\current\\myapp.exe'], stdout=PIPE, 
stdin=PIPE, stderr=PIPE)
stdout_data = p.communicate(input='13 20180212.log 20180212.txt 
20180212fix.txt'.encode("utf-8"))[0]
stdout = p.communicate()[0]
print ("STDOUT:{}".format(stdout))`

But I want to replace these hard coded values with variables and it is not working successfully as follows:

Output prints as follows - and application is open but nothing is happening

C:\Program Files\app\Bin\current\myapp.exe ['13', '20180212.log', '20180212.txt', '20180212fix.txt']

option_num = "13"
date_log = "20180212.log"
date_text = "20180212.text"
date_fix = "20180212.fix"

arguments = [option_num, date_log, date_text, date_fix]
print (arguments)

command = [program_name]
command.extend(arguments)

output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
print (output)

No error, the app just receives it all as one line of input instead of processing a return or enter after each variable - and if I look at the process open in Windows Process Explorer, it shows all the parameters being passed at once

Community
  • 1
  • 1
Sherri
  • 21
  • 2

1 Answers1

0

There are multiple things going on around here. The bottom line of the behavior your describing is that myapp.exe which apparently receiving and processing inputs through stdin, but with that change you did not only switch to using variable, but also stopped passing them in through stdin and instead call your application with these values as positional argument.

If you indeed wanted to pass each value on a new line, you could do something like (depends on how you want to handle your grouping and what else you expect form the code). Variation on your code:

values = (option_num, date_log, date_text, date_fix)
input_data = '\n'.join(values)
stdout_data = p.communicate(input=input_data.encode('utf-8'))[0]
print("STDOUT:{}".format(stdout_data))

or (use writelines method over generator based off of values):

values = (option_num, date_log, date_text, date_fix)
p.stdin.writelines(((v+'\n').encode('utf-8') for v in values))
stdout_data = p.communicate()[0]
print("STDOUT:{}".format(stdout_data))

or (similar to above, using write of stdin file-like object created for PIPE):

values = (option_num, date_log, date_text, date_fix)
for v in values:
    input_line = (v+'\n').encode('utf-8')  # append newline and convert to bytes
    p.stdin.write(input_line)
stdout_data = p.communicate()[0]
print("STDOUT:{}".format(stdout_data))

But, that is the other thing I wanted to mention, that is not what the initial example did. The values were passed space (and not newline) delimited. So it that work, the receiving myapp.exe might actually expect spaces and not newlines (or any whitespace character in which case both were OK).

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
  • Thank you I will try that as well, I figured it out right after posting, all I had to to was add a space after the variable. It is a very old legacy written in C++ and expects keyboard input after each variable is passed. the space works as an enter. – Sherri Mar 01 '18 at 21:20