ritratt ritratt - 7 months ago 47
Python Question

How to run a DOS batch file in background using Python?

How to run a DOS batch file in background using Python?

I have a test.bat file in say C:\

Now, I want to run this bat file using python in the background and then I want to return to the python command line.

I run the batch file using

subprocess.call('path\to\test.bat')
from the python command line.
It runs the batch file in the same window as the python command line.

If still not clear/ TL.DR-

What is happening:

>>>subprocess.call('C:\test.bat')
(Running test.bat. Can't use python in the same window)


What I want:

>>>subprocess.call('C:\test.bat')
(New commandline window created in the background where test.bat runs in parallel.)
>>>

Answer

This seems to work for me:

import subprocess

p = subprocess.Popen(r'start cmd /c C:\test.bat', shell=True)

p.wait()

print 'done'