I’d like to test if a program is executable or not (i.e., in this case, whether it is in the PATH) in a python script.
Any suggestions as to how to do this in a way that does not raise a Python error?
Michael
C. Michael Barton, Professor of Anthropology
Director of Graduate Studies
School of Human Evolution & Social Change
Center for Social Dynamics & Complexity
Arizona State University
Phone: 480-965-6262
Fax: 480-965-7671
www: <www.public.asu.edu/~cmbarton>
Michael Barton wrote:
I'd like to test if a program is executable or not (i.e., in this
case, whether it is in the PATH) in a python script.
Any suggestions as to how to do this in a way that does not raise a
Python error?
This is probably about as good as you're likely to get:
import os
import os.path
def find_exe(name):
try:
path = os.getenv("PATH")
if path == None:
path = os.defpath
for dir in path.split(os.pathsep):
file = os.path.join(dir, name)
if os.access(file, os.X_OK):
return file
except:
pass
return None
This isn't 100% reliable, but nothing is. Ultimately, you don't really
know if you can execute something until you try. Particularly on
Windows.
--
Glynn Clements <glynn@gclements.plus.com>