As part of my work, need to run simulations which are driven by python’s sub process module is the work horse for this. But there’s a problem.
On Windows,the subprocess module doesn’t look in the PATH unless you pass shell=True. However, shell=True can be a security risk if you’re passing arguments that may come from outside your program.
To make subprocess nonetheless able to find the correct executable, we can use shutil.which.
Suppose the executable in your PATH is named data_loader:
subprocess.call([shutil.which('data_loader'), arg1, arg2])
shutil.which(cmd, mode=1, path=None) Given a command, mode, and a PATH string, return the path which conforms to the given mode on the PATH, or None if there is no such file.
Increasingly I am loving the shutil library it’s so versatile..