Shutil to Rescue

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..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s