0

I start a subprocess from my interactive python shell:

p = subprocess.Popen(["my_executable.exe", "my_input.txt", "my_output.pdf"], stdin=subprocess.PIPE, text=True)

my_executable.exe is a program which generates a PDF file. But the program asks for a confirmation which pops up after the command from above:

enter image description here

Hitting the enter key on the keyboard works fine (hence what I try below should work?) but since I need to apply my_executable.exe to thousands of files, I need to send that OK-click to the subprocess via python. I tried the following:

enter image description here

But as you can see, I do not even get back to my command prompt (>>>). That horizontal bar keeps blinking and the OK button is still there / has not been clicked. I have also tried input="\n". This is Windows 7 / cmd.exe / python 3.7 interactive shell.

asked Jun 19, 2019 at 8:54
3
  • seems like the program is not meant to be used from console hence sending "\r\n" to it's stdin won't do anything... you need to send it a keystroke either globally with a module like keyboard if your window is in focus as it runs or by using win32api which will allow you to send a keystroke to background windows Commented Jun 19, 2019 at 9:48
  • I "solved" the problem via "p.terminate()". The PDF is there, so I think/hope, "terminate()" is shutting down the program properly. Commented Jun 21, 2019 at 13:50
  • @Robert terminate tells the program to terminate its run, so its now the programs responsibility to decide what to do next. Some will still hang (because they "can't" exit yet). Some will exit immediately, and some will do a few extra stuff (e.g. save PDF) and exit. This is NOT a generic solution. It works for your case Commented Jul 15, 2022 at 5:23

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.