Welcome, guest | Sign In | My Account | Store | Cart

This utility searches all the paths in a semi-colon delimited environment variable list for files matching a given filespec. By default, PATH is used for the enviroment. For example, on my computer

C:>where note*.exe C:\WINNT\system32\notepad.exe C:\WINNT\NOTEPAD.EXE

Python, 32 lines
 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
"""Searches a path for a specified file.
"""

__author__ = "Bill McNeill <billmcn@speakeasy.net>"
__version__ = "1.0"

import sys
import os
import os.path
import glob

def help():
	print """\
where (Environment) Filespec

Searches the paths specified in Environment for all files matching Filespec.
If Environment is not specified, the system PATH is used.\
"""

if len(sys.argv) == 3:
	paths = os.environ[sys.argv[1]]
	file = sys.argv[2]
elif len(sys.argv) == 2:
	paths = os.environ["PATH"]
	file = sys.argv[1]
else:
	help()
	sys.exit(0)

for path in paths.split(";"):
	for match in glob.glob(os.path.join(path, file)):
		print match

This is useful for finding which executable file will be loaded by default. The optional Environment variable is handy for debugging build environments.

1 comment

Andy Kirkpatrick 18 years, 8 months ago  # | flag

Cross platform tweak. To support other platforms,

for path in paths.split(os.pathsep):

I know UNIX generally has this functionality included, but just in case you want it python-native. Now I'm wondering if the mac uses a variable called PATH...