0

I can call another program in Python using

subprocess.call(["do_something.bat"])

I want to know if I can collect the stdin input of do_something.bat?

do_something.bat is a launcher for a Java program, the Java program will prompt the user to enter project specific information such as project name, version, and will generate a project skeleton according to the user input.

I use python to call this do_something.bat, and after it generates all the projects files, I need continue to go to a specific directory under project root, but that requires to know the project name, can I get the project name that the user previously entered?

Sawyer
  • 15,581
  • 27
  • 88
  • 124
  • 1
    can you not collect those values yourself and then do whatever `do_something.bat` does or execute it? – SilentGhost Nov 12 '12 at 15:04
  • is this answer your question :http://stackoverflow.com/questions/2502833/python-store-output-of-subprocess-call-in-a-string ? – lucasg Nov 12 '12 at 15:04
  • @SilentGhost That was what I was thinking - I suppose it's possible to replace that programs stdin and do it like that, but it seems rather a convoluted method... – Jon Clements Nov 12 '12 at 15:06
  • @SilentGhost `do_something.bat` only does a small portion of the whole work, and I have no way to change that. I need those inputs it had prompted the user to input. – Sawyer Nov 12 '12 at 15:06
  • Does the program only pull input directly from stdin, or does it also communicate directly with the TTY? The latter is almost certainly the case if it's doing a password prompt (and written by anyone moderately security-aware). [EDIT: Oh -- you're on Windows; might be moot there]. – Charles Duffy Nov 12 '12 at 15:11
  • Isn't it possible to prompt user in python script and pass those values as environment variables? I mean, who requests the input? The `do_something.bat` or a subroutine spawned by this .bat? – alex_jordan Nov 12 '12 at 15:12
  • @alex_jordan @Charles Duffy `do_something.bat` calls a java program, it requires the user to enter the project name and other project setting related parameters into command line prompt, then the java program will generate a project skeleton. I need to continue to do some things after that .bat launcher exits. for example I need to go to specific a directory inside project root, can't do that without knowing the project name, as it generates the directory as per project name. – Sawyer Nov 12 '12 at 15:28

2 Answers2

2

It depends a bit on how do_something.bat prompts the user.

If it simply reads from standard input your program can act as a go-between. It can prompt the output of do_something.bat, read the user's response, and then pipe the response back to the standard input of do_something.bat.

Otherwise, I do not think it is possible without adapting do_something.bat.

Hans Then
  • 10,935
  • 3
  • 32
  • 51
1

If you know what the exact parameters that the program will ask for and what order it will ask for them then you can collect the arguments yourself and forward them on to the subprocess.

eg.

from subprocess import Popen, PIPE

# get inputs
input1 = ...
input2 = ...

child = Popen("do_something.bat", stdin=PIPE)
# Send data to stdin. Would also return data from stdout and stderr if we set 
# those arguments to PIPE as well -- they are returned as a tuple
child.communicate("\n".join([input1, input2, ...]))

if child.returncode != 0:
    print "do_something.bat failed to execute successfully" 
Dunes
  • 37,291
  • 7
  • 81
  • 97