Check if a command is valid

Tim Roberts timr at probo.com
Tue Jul 13 02:18:48 EDT 2010


Kenny Meyer <knny.myer at gmail.com> wrote:
>
>I have to figure out if a string is callable on a Linux system. I'm
>actually doing this:
>...
>Never mind the code, because this is not the original.
>The side effect of subprocess.call() is that it *actually* executes
>it, but I just need the return code. What are better ways of doing
>this?

You can do what the "which" command does: split the PATH environment
variable into individual components, and see if the string maps to a file
in any directory in your PATH with the "execute" bit set.  That won't catch
shell macros, however.  You might be able to use "type" to do that.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list