Hi,
ich suche nach einem eleganten Weg (auf einem Linuxsystem) herauszufinden, ob es ein Programm (foo) auf meinem System gibt.
Für die bash würde ich wahrscheinlich so etwas schreiben:
if [ -x "$(which foo)" ]; then echo "jawollja"; else echo 'nepp'; fi
Kennt jemand eine elegantere Variante als (systemabhängig) den Output von os.system('which foo') zu parsen?
Ich hatte gehofft, in os etwas passendes zu finden...
Schönen Gruß!
Ist 'foo' in PATH?
Ich antworte mir mal selbst.flea hat geschrieben: ich suche nach einem eleganten Weg (auf einem Linuxsystem) herauszufinden, ob es ein Programm (foo) auf meinem System gibt.
Ich habe zwar immer noch nichts gefunden, aber folgende Funktion funktioniert ganz gut (relativ systemunabhängig):
>>> def inPath(cmd):
... if not cmd: return None
... env_path = os.getenv('PATH') or os.defpath
... for i in string.split(env_path, os.pathsep):
... path = i + os.sep + cmd
... if os.access(path, os.X_OK):
... return path
... return None
...
>>> print inPath('ls')
/bin/ls
>>> print inPath('lss')
None
>>>
Ciao!
Django schießt aus dem Knie, hier die Version ordentlich eingerückt:
Code: Alles auswählen
>>> def inPath(cmd):
... if not cmd: return None
... env_path = os.getenv('PATH') or os.defpath
... for i in string.split(env_path, os.pathsep):
... path = i + os.sep + cmd
... if os.access(path, os.X_OK):
... return path
... return None
...
>>> print inPath('ls')
/bin/ls
>>> print inPath('lss')
None
>>>