You are not logged in.
I'm messing around with python again and I want to run a command inside my program. That command relies on a package being installed in linux.
So, how can I do something like
which foo
=> /usr/bin/foo
which bar
=>
And know to use foo instead of bar inside my code? Both commands in python return
<subprocess.Popen object at 0x15cf210>
(I am using this command in my code):
foo = subprocess.Popen("which " + "foo", shell=True)
Thanks in advance
Offline
Offline
Well, if you want to rely on the user's $PATH then you could do this straight forward by using:
import os
a = os.system("which "+PROGRAM_NAME)
if a != 0:
use/check for fallback program
But note that which only looks in the paths defined in $PATH so it might miss on some.
Last edited by kowalski (2009-11-22 14:10:50)
He who says A doesn't have to say B. He can also recognize that A was false.
Offline
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
I ended up using:
check_ack = os.popen("which ack-grep")
if len(check_ack.readlines()) == 0:
Imperfect but it got the job done on short notice.
Offline
You could use subprocess' call or returncode stuff for this as well:
import subprocess
# which returns 1 on error / nothing found
if not subprocess.call(["which", "ack-grep"]):
# ack-grep was found ... do stuff
# Or, if you wanted the return value + output of which:
which = subprocess.Popen(["which", "ack-grep"], stdout=subprocess.PIPE)
# Any stuff output by running the which command
output = which.communicate()[0]
# The returncode of the which command
ret = which.returncode
Last edited by BetterLeftUnsaid (2009-11-22 17:47:36)
Offline