Reputation: 7331
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
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
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
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