1

I have created a Python script which needs to launch a .bat file based on some condition.

Python script location : \Component\myScript.py

Batch file location : \Component\MS20160825\toExecute.bat
The batch file internally uses some executables which are in \Component\bin\

How do I do following :

  1. Launch .BAT file from Python script so that .BAT executes successfully. BAT file should be able to find executables in \Component\bin\ directory to perform its task and produces desired result.

  2. Hold Python script execution until .BAT finished its execution.

  3. .BAT file has pause >nul statement. I need to bypass it, meaning when .BAT is executed from Python script it should not wait for user to press Enter rather it should terminate normally after executing second last statement. Because the same .BAT file needs to be executed multiple times.

3
  • Run the batch file from Python script with a parameter like NoPause and use in the batch file if /I not "%~1" == "NoPause" pause >nul. Run in a command prompt window call /? to get help on how to make use of batch file parameters. %0 references argument 0 which is the batch file itself. %~dp0 is for example drive and path to batch file ending with a backslash. That might be useful to reference other folders/files from within batch file with a path relative to path of batch file. Commented Aug 25, 2016 at 11:39
  • About relative paths: A path starting with a backslash like \Component\myScript.py is relative to root of current drive. A path starting with ..\ references the parent directory of current directory. And a path starting with .\ or with a folder or file name is relative to current directory. Commented Aug 25, 2016 at 11:42
  • Yes that was a typing mistake. Thanks Mofi for pointing it out. Commented Aug 26, 2016 at 18:14

2 Answers 2

2

Should solve all problems you are experiencing!

import subprocess

p = subprocess.Popen('batch.bat', shell=True, stdin=subprocess.PIPE)
stdout, stderr = p.communicate()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution. It worked but requires a small correction. i.e.
2

Thanks to all for their active suggestion.
With small correction following code worked for me:

import subprocess

batchFileLocation = 'Component\\MS20160825'
batchFileFullPath = os.path.join(batchFileLocation, 'toExecute.bat')

p = subprocess.Popen(os.path.abspath(batchFileFullPath), stdin=subprocess.PIPE, cwd = batchFileLocation)
stdout,stderr = p.communicate()

Here cwd argument is very much important, it needs to be updated with the location where batch file is placed then only the batch file will be able to execute correctly.

After that only batch file will be able to search for the binary (placed in a different directory like Component\bin\ in this case) required for its execution.

Comments

Your Answer

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.