user2426316
user2426316

Reputation: 7331

How can I surpass "Press any key to continue ..." in python?

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?

Upvotes: 1

Views: 7505

Answers (3)

c.Parsi
c.Parsi

Reputation: 771

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()

Upvotes: 0

Silly
Silly

Reputation: 227

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

Upvotes: 0

glglgl
glglgl

Reputation: 91049

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.

Upvotes: 2

Related Questions