Python equivalent to 'which'?

David Ascher DavidA at ActiveState.com
Tue Jul 30 13:54:43 EDT 2002


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





More information about the Python-list mailing list