Run a .bat file using python code


I try to run a .bat file in Windows using Python script.

ask.bat file:

Application.exe work.xml

I write Python code :

import os
os.system("D:\xxx1\xxx2XMLnew\otr.bat ")

Output: when try to run the file its just give a blink of the command prompt, and the work is not performing.

Note: I try with alternate slash also , but it is not working.

And I also want to save output of the file in another file.

Can anyone suggest how can I make the script runnable.

4/7/2014 8:25:15 AM

This has already been answered in detail on SO. Check out this thread, It should answer all your questions: Executing a subprocess fails

I've tried it myself with this code:

from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()


echo Hello World!

I've got the example from the aforementioned thread.

5/23/2017 11:54:43 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow