Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.)
>>>
like image 279
ritratt Avatar asked Apr 01 '26 07:04

ritratt


1 Answers

This seems to work for me:

import subprocess

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

p.wait()

print 'done'
like image 121
martineau Avatar answered Apr 02 '26 20:04

martineau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!