Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python equivalent to 'which'?

0 views
Skip to first unread message

eg

unread,
Jul 30, 2002, 12:36:07 PM7/30/02
to
Is their a python equivalent to the bash shell command 'which'? I
haven't found it, and 'which' is such a common word that it may be
lost in the noise in my dejanews searches :(

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

Michael Gilfix

unread,
Jul 30, 2002, 1:21:24 PM7/30/02
to
Seems like that might make a good convenience function in the
os.path module. I'm sure many people have wanted such a function
if they're using python for pure scripting. Perhaps you should try
submitting a short patch...? Worst case, at least it'll get stored in
the system.

-- 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

Sean 'Shaleh' Perry

unread,
Jul 30, 2002, 1:01:23 PM7/30/02
to

Trent Mick

unread,
Jul 30, 2002, 1:55:04 PM7/30/02
to
[eg wrote]

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

David Ascher

unread,
Jul 30, 2002, 1:54:43 PM7/30/02
to
Sean 'Shaleh' Perry wrote:

> On 30-Jul-2002 Michael Gilfix wrote:
>
>> Seems like that might make a good convenience function in the
>>os.path module. I'm sure many people have wanted such a function
>>if they're using python for pure scripting. Perhaps you should try
>>submitting a short patch...? Worst case, at least it'll get stored in
>>the system.
>>
>
>
> I wrote that in about a minute and a half.
>
> 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.
>

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.
"""


Sean 'Shaleh' Perry

unread,
Jul 30, 2002, 1:36:14 PM7/30/02
to

Michael Gilfix

unread,
Jul 31, 2002, 10:21:03 AM7/31/02
to
This module looks pretty good and does seem kinda appropriate. I wonder
why Tent Mick hasn't suggested it?

-- 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)

Michael Gilfix

unread,
Jul 31, 2002, 10:14:05 AM7/31/02
to
On Tue, Jul 30 @ 10:36, Sean 'Shaleh' Perry wrote:
> I wrote that in about a minute and a half.

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

0 new messages