ActiveState Code

Recipe 576823: Prints full name of all occurrences of given filename in your PATH


Simple program to print the full name of all occurrences of the given filename in your PATH. Kind of like the Unix "which" utility, but works for DLLs and other files as well.

Usage: findinpath.py filename

Python
 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
"""Prints full name of all occurrences of given filename in your PATH.

Usage: findinpath.py filename"""

import os
import sys

def main():
    if len(sys.argv) < 2:
        print __doc__
        return 2
    filename = sys.argv[1]

    status = 1
    sep = ';' if sys.platform == 'win32' else ':'

    for path in os.environ['PATH'].split(sep):
        fullname = os.path.join(path, filename)
        if os.path.exists(fullname):
            print fullname
            status = 0

    return status

if __name__ == '__main__':
    sys.exit(main())

Comments

  1. 1. At 1:42 a.m. on 30 jun 2009, Michal Niklas said:

    It is similar to my recipe: http://code.activestate.com/recipes/576522/ (my is Windows specific and for .dll tries to load it)

Sign in to comment