1

In my project I am starting a .bat file from a python script. Just like this:

os.system("testfile.bat")

When this `testfile.bat is finished it ends with the prompt Press any key to continue .... I would like my python script to surpass this prompt and somehow simulate the keypress.

How can I achieve that? I already implemented this functionality by using a subprocess but as it turned out a subprocess is not suited for the context of my project (has something to do with printing to the console). Any ideas?

2
  • 1
    Does 'testfile.bat' have a pause at the end? – Rushy Panchal Sep 12 '13 at 13:21
  • 1
    The pause gives the "Press any key to continue ..." prompt. Just remove the pause from the batch file. – Rushy Panchal Sep 12 '13 at 13:27
2

Using the subprocess module is always suited and superior to os.system.

Just do

sp = subprocess.Popen("testfile.bat", stdin=subprocess.PIPE)
sp.stdin.write("\r\n") # send the CR/LF for pause
sp.stdin.close() # close so that it will proceed

and you should be done.

1
  • That's assuming that testfile.bat doesn't have any other programs that read standard input. OP doesn't say that testfile.bat is non-interactive. – Robᵩ Sep 12 '13 at 13:46
0

Add >nul to the end of the pause commend (In the batch file.) it will not print "Press any key to continue.

0

If you have "pause" at the end of your Bat file just remove it, it will continue executing the rest of python script after the subprocess is done.

p = subprocess.Popen("testfile.bat", shell=False, \
                          cwd=path_to_exe)

p.wait()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.