2

I have a python script that needs to start a .bat file. The .bat file runs indefinitely, and, ideally, never returns.

There's no problem running a foreign executable or .bat from within a python script, but all the implementations I've found run it as a child and wait for it to return.

Can I run an external script and detach from it, allowing it to run merrily, and have the python script continue to run?

*Edit: * using Popen runs the batch file in the same command prompt window - as the batch file never returns, the python app us unable to continue running. Is there a way to run a batch file in a new command prompt window?

1 Answer 1

3

you can use subprocess:

import subprocess
subprocess.Popen(["mybatchfile.bat"])

This starts the batch file and will not wait for to complete. If you want to verify that process is created, you can run:

p=subprocess.Popen(["mybatchfile.bat"])
if p.pid > 0:
   print "Process created with PID:", p.pid
Sign up to request clarification or add additional context in comments.

1 Comment

From the Python Documentation: On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

Your Answer

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.