I have a a file structure like the following (Windows):
D:\
dir_1\
batch_1.bat
dir_1a\
batch_2.bat
dir_2\
main.py
For the sake of this question, batch_1.bat
simply calls batch_2.bat
, and looks like:
cd dir_1a
start batch_2.bat %*
Opening batch_1.bat
from a command prompt indeed opens batch_2.bat
as it's supposed to, and from there on, everything is golden.
Now I want my Python file, D:\dir_2\main.py
, to spawn a new process which starts batch_1.bat
, which in turn should start batch_2.bat
. So I figured the following Python code should work:
import subprocess
subprocess.Popen(['cd "D:/dir_1"', "start batch_1.bat"], shell=True)
This results in "The system cannot find the path specified" being printed to my Python console. (No error is raised, of course.) This is due to the first command. I get the same result even if I cut it down to:
subprocess.Popen(['cd "D:/"'], shell=True)
I also tried starting the batch file directly, like so:
subprocess.Popen("start D:/dir_1/batch_1.bat", shell=True)
For reasons that I don't entirely get, this seems to just open a windows command prompt, in dir_2
.
If I forego the start
part of this command, then my Python process is going to end up waiting for batch_1
to finish, which I don't want. But it does get a little further:
subprocess.Popen("D:/dir_1/batch_1.bat", shell=True)
This results in batch_1.bat
successfully executing... in dir_2
, the directory of the Python script, rather than the directory of batch_1.bat
, which results in it not being able to find dir_1a\
and hence, batch_2.bat
is not executed at all.
I am left highly confused. What am I doing wrong, and what should I be doing instead?