0

I have a batch file, which I use to load some pre-build binaries to control my device. It's command is:

cd build
java -classpath .;..\Library\mfz-rxtx-2.2-20081207-win-x86\RXTXcomm.jar -
Djava.library.path=..\Library\mfz-rxtx-2.2-20081207-win-x86 tabotSample/Good1
pause

Now, I want to run the batch file using Python, and I tried os.system(batch,bat), and I tried using Popen

import os from subprocess import Popen os.popen("cd TAbot") r=os.popen("hello.bat")

However, the python console(Anaconda python 2.7) seems like executed the code, but returns nothing, and nothing happens. I want to run this batch file from python, please help me. by the way, I tried popen for another batch file like, echo Hello but nothing happens.

asked May 30, 2017 at 8:07
2
  • As is, cd build won't work in general. To depend on the working directory to find resources relative to a script is a mistake. Make it relative to the directory of the batch file itself, instead of depending on the working directory. For example, if the "build" directory is in the same directory as the batch file, you can use cd /d "%~dp0build", where the batch filename is the %0 parameter, and we're getting the [d]rive and base [p]ath as %~dp0. Commented May 30, 2017 at 8:24
  • It didn't work, I don't know why every time I use popen to run .bat file nothing happens. Commented May 30, 2017 at 8:47

4 Answers 4

2

Here is the simple solution.

from subprocess import Popen
import subprocess
def run_batch_file(file_path):
 Popen(file_path,creationflags=subprocess.CREATE_NEW_CONSOLE)
run_batch_file('file_name.bat')

file_name.bat

echo .bat file running from python
pause
answered May 30, 2017 at 9:00
Sign up to request clarification or add additional context in comments.

Comments

1

You can also use this

import subprocess
subprocess.call(["C:\\temp\\test.bat"], shell=False)

test.bat

copy "C:\temp\test.txt" "C:\temp\test2.txt"
answered May 30, 2017 at 9:17

6 Comments

It seems that batch file is working, however it need to choose Y/N, how do I input that? or pre input this key?
You can pass parameters for example subprocess.call(["C:\\temp\\test.bat", "Y"] or subprocess.call(["C:\\temp\\test.bat", "N"]. In the bat file you have access to this parameters with %0, %1, ... . http://www.robvanderwoude.com/parameters.php
I tried subprocess.call(["C:\\temp\\test.bat", "Y"] but, it didn't work. Sorry, I'm really new to this.
I run xcopy "C:\temp\test.txt" "C:\temp\test2.txt" on cmd, and it asked me Y/N/A , it will be copied when I time Y or A. I want to do all the process by python. When I run subprocess.call(["C:\\temp\\test.bat", "Y"] in python, the kernel just stacked.
sorry my fault xcopy needs user interaction the example is not working correctly. try copy "C:\temp\test.txt" "C:\temp\test2.txt" and subprocess.call(["C:\\temp\\test.bat"])
|
0

I think this should work like this:

batch.py

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

test.bat

echo Hello World!
pause
answered May 30, 2017 at 8:21

Comments

0

Here many guys suggested very useful solutions, but I want to point the importance of where is the program located. (Bat file is usually made for automation task to reduce time and this has high probability to work some task related path)

import subprocess
os.chdir("YOUR TARGET PATH")
exit_code = subprocess.call(FILEPATH)# FILEPATH is from the standpoint on YOUR TARGET PATH
answered Mar 29, 2019 at 4:23

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.