UNIX users are familiar with the which command. Given an argument called name, it checks the system PATH environment variable, to see whether that name exists (as a file) in any of the directories specified in the PATH. (The directories in the PATH are colon-separated on UNIX and semicolon-separated on Windows.)
This recipe shows how to write a minimal which command in Python. It has been tested on Windows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
from __future__ import print_function # which.py # A minimal version of the UNIX which utility, in Python. # Author: Vasudev Ram - www.dancingbison.com # Copyright 2015 Vasudev Ram - http://www.dancingbison.com import sys import os import os.path import stat def usage(): sys.stderr.write("Usage: python which.py name\n") sys.stderr.write("or: which.py name\n") def which(name): found = 0 for path in os.getenv("PATH").split(os.path.pathsep): full_path = path + os.sep + name if os.path.exists(full_path): """ if os.stat(full_path).st_mode & stat.S_IXUSR: found = 1 print(full_path) """ found = 1 print(full_path) # Return a UNIX-style exit code so it can be checked by calling scripts. # Programming shortcut to toggle the value of found: 1 => 0, 0 => 1. sys.exit(1 - found) def main(): if len(sys.argv) != 2: usage() sys.exit(1) which(sys.argv) if "__main__" == __name__: main()
This utility is written in Python 2.7.8 and tested on Windows 7, so far, though it should also work on most other platforms where Python runs, particularly UNIX, Linux and Mac OS X, since it uses no platform-specific Python code or libraries, and in fact, it explicitly uses features of the os.path module for portability, such as os.path.sep, os.path.pathsep, and os.path.exists.
More information, and example runs and outputs of the program, are available at this blog post:
Actually this is
You're right, thanks for pointing it out. I had not thought of that at the time of writing the program. somehow.
-a or --all prints all programs that match (with their paths), not just the first.
I'll have to make a comment in my blog post about this too.
However, UNIX being what it is, even without my changing the code to support both the -a option and no -a option (correctly), people can easily work around the issue with this (on UNIX or a Windows that has a sed):
which foo | sed 1q
which will print only the first line of output, and when they want all lines, they can just do:
Hooray for UNIX! :)
I wrote another post with a corrected version of which.py, which now works for both the single and the -a / --all case: