In Python, is there a portable and simple way to test if an executable program exists?
By simple I mean something like the which
command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with Popen
& al and see if it fails (that's what I'm doing now, but imagine it's launchmissiles
)
you can tell if a file exists with the os module. an executable in particular seems quite unportable considering lots of things are executable on nix that aren't on windows and vice versa.
Easiest way I can think of:
Edit: Updated code sample to include logic for handling case where provided argument is already a full path to the executable, i.e. "which /bin/ls". This mimics the behavior of the UNIX 'which' command.
Edit: Updated to use os.path.isfile() instead of os.path.exists() per comments.
Edit:
path.strip('"')
seems like the wrong thing to do here. Neither Windows nor POSIX appear to encourage quoted PATH items.Added windows support