I want to run .bat and .vbs files from Python

Asked 1 years ago, Updated 1 years ago, 342 views

Assume the file is placed as shown in the image below:

Enter a description of the image here

The contents of each file are as follows:

python_test.py
python_tes.py calls the .bat or .vbs files.

from os import path
import subprocess

# Call the pattern 1.bat file
runPath=path.join(path.dirname(__file__), 'run.bat')
subprocess.Popen (runPath)

# Call Pattern 2.vbs File
# vbsPath=path.join(path.dirname(__file__), 'noConsoleRun.vbs')
# subprocess.Popen(r'wscript"'+vbsPath+'")

run.bat
run.bat sets the environment variable and runs the exe file.

SETLOCAL
SET PATH = %PATH%;..\..\samples\external\opencv\bin;..\..\bin;
test.exe

noConsoleRun.vbs
Use noConsoleRun.vbs to erase the black console window that appears in the .bat file.

Sets=CreateObject("Wscript.Shell")
ws.run "cmd/crun.bat", vbhide

In fact, when you run python_test.py, you get the following error when you run the .bat file in pattern 1:

 'test.exe' is not recognized as an internal or external command, operable program or batch file.

The .vbs file execution in pattern 2 does not produce any errors, but nothing is done and there is no GUI other than the console.

Each will boot normally by manually double-clicking run.bat or noConsoleRun.vbs without running from Python.

How can I start with Python?

python

2022-11-28 21:24

1 Answers

The comments seem to have solved it, so I'll write the answer.

I think it will only work with the batch file measures I showed you later :

Before running test.exe at the beginning or anywhere of run.bat, insert CD/D% to dp0 to change the directory where the batch file resides to the current directory as follows:

CD/D% to dp0
SETLOCAL
SET PATH = %PATH%;..\..\samples\external\opencv\bin;..\..\bin;
test.exe

Could you possibly start the process with Python or VBS?In case you do, refer to the following article to change the directory where the script is located at the beginning of the Python script to the current directory.
Python secures the current directory to the script directory

importos
os.chdir(os.path.dirname(os.path.abspath(__file__)))


2022-11-28 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.