I could grab the PATH environment variable and walk all the paths, but
I'm hoping python has already done it for me!
Thanks
Eric
-- Mike
On Tue, Jul 30 @ 10:01, Sean 'Shaleh' Perry wrote:
> >>> def which(exe):
> ... for d in string.split(os.getenv('PATH'), ':'):
> ... name = os.path.join(d, exe)
> ... if os.path.isfile(name): return name
> ... return None
> ...
> >>> which('bash')
> '/bin/bash'
> >>> which('foo')
> that's all 'which' does (-:
--
Michael Gilfix
mgi...@eecs.tufts.edu
For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html
I wrote one that works on Windows and Unix. On Windows it understands
the AppPaths registration that can be done to make specific application
launchable without being on the PATH. It understands PATHEXT etc. It can
be used as a module or as a script.
http://starship.python.net/~tmick/
Cheers,
Trent
--
Trent Mick
Tre...@ActiveState.com
Doing 'which' right is actually fairly complicated -- Trent Mick has the
best version I know of at: http://starship.python.net/~tmick/
"""
GNU which is a common command line app on Unix. There are Windows ports
(for example, the one in the Cygwin distribution), though I find the
ones I have seen to be a little stupid. They don't understand the use of
the PATHEXT environment variable. They don't support the useful '-a'
option to listing all matches on the current PATH.
This which has the following features:
it is portable (Windows, Linux);
it understands PATHEXT on Windows;
it can print all matches on the PATH;
it can note "near misses" on the PATH (e.g. files that match but may
not, say, have execute permissions); and
it can be used as a Python module.
"""
-- Mike
On Tue, Jul 30 @ 10:54, David Ascher wrote:
> Doing 'which' right is actually fairly complicated -- Trent Mick has the
> best version I know of at: http://starship.python.net/~tmick/
>
> """
> GNU which is a common command line app on Unix. There are Windows ports
> (for example, the one in the Cygwin distribution), though I find the
> ones I have seen to be a little stupid. They don't understand the use of
> the PATHEXT environment variable. They don't support the useful '-a'
> option to listing all matches on the current PATH.
>
> This which has the following features:
>
> it is portable (Windows, Linux);
> it understands PATHEXT on Windows;
> it can print all matches on the PATH;
> it can note "near misses" on the PATH (e.g. files that match but may
> not, say, have execute permissions); and
> it can be used as a Python module.
> """
`-> (DavidA)
Yes, and I could hack together a version in a minute as well. But
someone else couldn't. And it seems like a fairly common and useful
function. A full implementation is another story.
> Note it has a large assumption that PATH is defined and it is separated by
> colons. This makes it only truly useful on a UNIX box. of course 'which' is a
> UNIXism as well. My point is, the python library is for the most part OS
> agnostic so I do not see this fitting in too well.
I didn't mean for the function to go in as is. I meant the general functionality.
Perhaps the starship version in the other thread is the best solution.
-- Mike