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.

Robert
  • 432
  • 1
  • 4
  • 15
  • 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 – AntiMatterDynamite Jun 19 '19 at 09:48
  • I "solved" the problem via "p.terminate()". The PDF is there, so I think/hope, "terminate()" is shutting down the program properly. – Robert Jun 21 '19 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 – CIsForCookies Jul 15 '22 at 05:23

0 Answers0