On Mon, Jul 12, 2010 at 6:29 PM, Kenny Meyer <knny.m...@gmail.com> wrote: > Hello, > > I have to figure out if a string is callable on a Linux system. I'm
"callable" seems vague. Is a command string with invalid arguments but a valid executable "callable"? If no, then there's no general way to test "callability" without actually running the command. > actually doing this: > > def is_valid_command(command): > retcode = 100 # initialize > if command: > retcode = subprocess.call(command, shell=True) > if retcode is 0: That should be `== 0`, not `is 0`. The fact that `is 0` just so happens to work is an implementation detail. > print "Valid command." > else: > print "Looks not so good..." > > is_valid_command("ls") > > 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. Well, you're not gonna be able to get the command's return code without actually running it (unless perhaps you're referring to a return code from the shell itself?). > What are better ways of doing this? One idea: from shlex import split as shell_tokenize from subprocess import check_output def is_valid_command(command): try: executable = shell_tokenize(command)[0] except (ValueError, IndexError):# invalid shell syntax return False return bool(check_output(['which', executable]))# on the PATH? Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list