[GRASS-dev] python question - how to test if file is executable in
path?
Glynn Clements
glynn at gclements.plus.com
Sat Jul 19 18:34:29 EDT 2008
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 at gclements.plus.com>
More information about the grass-dev
mailing list