Skip to content Skip to sidebar Skip to footer

Running A Batch File In Another Directory In Python

I want to run mybat.bat file located in MyFolder which is different from the current directory. I used the following code: subprocess.Popen(['mybat', MyArg], cwd=

Solution 1:

The working directory is changed only in the child process i.e., cwd=MyFolder does not make os.path.join(MyFolder, "mybat.bat") available. Try:

p = Popen([os.path.join(MyFolder, "mybat.bat"), MyArg], cwd=MyFolder)

You could use %~dp0 inside your bat-file, to get the directory where the bat-file resides instead of cwd=MyFolder as @eryksun suggested.

Solution 2:

adding shell=True to command solved the problem.

Post a Comment for "Running A Batch File In Another Directory In Python"