74

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 slashes also, but it is not working.

bad_coder
13.3k20 gold badges60 silver badges95 bronze badges
asked Mar 29, 2011 at 7:37
3
  • 1
    Did you try escaping the backslash? Try: os.system("D:\\xxx1\\xxx2XMLnew\\otr.bat ") Commented Mar 29, 2011 at 7:58
  • use / instead of \ if you dont want to escape it Commented Oct 31, 2017 at 14:33
  • 1
    You just need to add a r before the quote: r"D:\\xxx1\\xxx2XMLnew\\otr.bat " Try: import os os.system(r"D:\xxx1\xxx2XMLnew\otr.bat ") Commented Oct 22, 2022 at 18:03

10 Answers 10

56

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 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.

answered Mar 29, 2011 at 7:49
Sign up to request clarification or add additional context in comments.

8 Comments

Hi das_weezul, Thanks for the info., i try what is mentioned there but for me the same problem persists, on executing,a blink of the app. is coming then went out, no work perform
while running this code i came around an error:" Windows Error 2: The system cannot find the file specified", but the file path is correct 100% i switch the folder location to test what u said.... code is from subprocess import Popen p = Popen("test.bat", cwd=r"C:\XMLnew") stdout, stderr = p.communicate()
@Silver: Popen() can't find the test.bat file. In the docs it says "If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd."
@martineau He used the path "C:\XMLnew" which is absolute, so that shouldn't be the problem
@das_weezul: Yes, but the "test.bat" is relative.
|
34
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
answered Dec 10, 2012 at 15:05

2 Comments

shell=True is what allows you to run a .bat file instead of an .exe.
@BobStein-VisiBone no, it's not. You can call a .bat file from Popen without passing shell=True, but as martineau noted in the answer above, you must specify the absolute path to the .bat file.
16

Replace \ with / in the path

import os
os.system("D:/xxx1/xxx2XMLnew/otr.bat ")
Jason Sturges
16k14 gold badges63 silver badges82 bronze badges
answered Jan 6, 2012 at 17:13

1 Comment

i recommend using os.path.normpath filepath="D:/xxx1/xxx2XML/otr.bat" p = Popen(filepath, shell=True, stdout = subprocess.PIPE) stdout, stderr = p.communicate() print process.returncode # is 0 if success
8

Probably the simplest way to do this is ->

import os
os.chdir("X:\Enter location of .bat file")
os.startfile("ask.bat")
answered Nov 23, 2017 at 13:57

1 Comment

Not sure about other operating systems, but works on windows :)
5
  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
    
answered Mar 9, 2016 at 2:21

Comments

4

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")
answered Sep 18, 2018 at 8:31

Comments

2

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)
answered Nov 7, 2018 at 8:55

Comments

1

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"
answered May 7, 2018 at 5:43

Comments

0

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.} 
stefan
10.4k4 gold badges55 silver badges91 bronze badges
answered Jun 17, 2013 at 15:13

Comments

0

If your .bat file is in same directory:

import os
os.startfile("filename.bat")

You can also run Multiple .bat files parallelly this way

os.startfile("filename.bat")
os.startfile("filename2.bat")

--
If you want to change directory before running .bat file, run this command:

os.chdir("X:\directory_inwhich_the_code_will_run")
answered Jun 22, 2023 at 21:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.