Run a .bat file using python code Run a .bat file using python code python python

Run a .bat file using python code


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:

batchtest.py

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

batch.bat

echo Hello World!pause

I've got the batchtest.py example from the aforementioned thread.


import subprocessfilepath="D:/path/to/batch/myBatch.bat"p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE)stdout, stderr = p.communicate()print p.returncode # is 0 if success


Replace \ with / in the path

import osos.system("D:/xxx1/xxx2XMLnew/otr.bat ")