Run a .bat file using python code

PythonBatch File

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

This has already been answered in detail on SO. Check out this thread, It should answer all your questions: [https://stackoverflow.com/questions/1818774/python-subprocess][1]

I've tried it myself with this code:

batchtest.py

from subprocess import Popen
p = 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. [1]: https://stackoverflow.com/questions/1818774/python-subprocess

Solution 2 - Python

import subprocess

filepath="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

Solution 3 - Python

Replace \ with / in the path

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

Solution 4 - Python

  1. It is better to write .bat file in such way that its running is not dependent on current working directory, i.e. I recommend to put this line at the beginning of .bat file:

     cd "%~dp0"
    
  2. Enclose filepath of .bat file in double quotes, i.e.:

     os.system('"D:\\x\\so here can be spaces\\otr.bat" ["<arg0>" ["<arg1>" ...]]')
    
  3. To save output of some batch command in another file you can use usual redirection syntax, for example:

     os.system('"...bat" > outputfilename.txt')
    

Or directly in your .bat file:

    Application.exe work.xml > outputfilename.txt

Solution 5 - Python

Probably the simplest way to do this is ->

import os
os.chdir("X:\Enter location of .bat file")
os.startfile("ask.bat")

Solution 6 - Python

You are just missing to make it raw. The issue is with "". Adding r before the path would do the work :)

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

Solution 7 - Python

So I do in Windows 10 and Python 3.7.1 (tested):

import subprocess
Quellpfad = r"C:\Users\MeMySelfAndI\Desktop"
Quelldatei = r"\a.bat"
Quelle = Quellpfad + Quelldatei
print(Quelle)
subprocess.call(Quelle)

Solution 8 - Python

python_test.py

import subprocess
a = subprocess.check_output("batch_1.bat")
print a

This gives output from batch file to be print on the python IDLE/running console. So in batch file you can echo the result in each step to debug the issue. This is also useful in automation when there is an error happening in the batch call, to understand and locate the error easily.(put "echo off" in batch file beginning to avoid printing everything)

batch_1.bat

echo off	
echo "Hello World" 
md newdir 
echo "made new directory"

Solution 9 - Python

If you are trying to call another exe file inside the bat-file. You must use SET Path inside the bat-file that you are calling. set Path should point into the directory there the exe-file is located:

set PATH=C:\;C:\DOS     {Sets C:\;C:\DOS as the current search path.} 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSilverView Question on Stackoverflow
Solution 1 - Pythondas_weezulView Answer on Stackoverflow
Solution 2 - Pythonuser1176501View Answer on Stackoverflow
Solution 3 - PythonraghuramView Answer on Stackoverflow
Solution 4 - PythontavView Answer on Stackoverflow
Solution 5 - Pythonoutcast_dreamerView Answer on Stackoverflow
Solution 6 - PythonPrayson W. DanielView Answer on Stackoverflow
Solution 7 - PythonmerlinuweView Answer on Stackoverflow
Solution 8 - PythonNeoView Answer on Stackoverflow
Solution 9 - Pythonuser2493936View Answer on Stackoverflow