portable way of locating an executable (like which)
Jason Swails
jason.swails at gmail.com
Thu Sep 20 18:04:41 EDT 2012
On Thu, Sep 20, 2012 at 5:06 PM, Gelonida N <gelonida at gmail.com> wrote:
> I'd like to implement the equivalent functionality of the unix command
> /usr/bin/which
>
> The function should work under Linux and under windows.
>
> Did anybody already implement such a function.
> If not, is there a portable way of splitting the environment variable PATH?
>
I've used the following in programs I write:
def which(program):
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.getenv("PATH").split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
IIRC, I adapted it from StackOverflow. I know it works on Linux and Mac OS
X, but not sure about windows (since I don't know if PATH works the same
way there).
HTH,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120920/49d56d86/attachment.html>
More information about the Python-list
mailing list