Recipe2.19.Finding Files Given a Search Path and a Pattern


Recipe 2.19. Finding Files Given a Search Path and a Pattern

Credit: Bill McNeill, Andrew Kirkpatrick

Problem

Given a search path (i.e., a string of directories with a separator in between), you need to find all files along the path whose names match a given pattern.

Solution

Basically, you need to loop over the directories in the given search path. The loop is best encapsulated in a generator:

import glob, os def all_files(pattern, search_path, pathsep=os.pathsep):     """ Given a search path, yield all files matching the pattern. """     for path in search_path.split(pathsep):         for match in glob.glob(os.path.join(path, pattern)):             yield match

Discussion

One nice thing about generators is that you can easily use them to obtain just the first item, all items, or anything in between. For example, to print the first file matching '*.pye' along your environment's PATH:

print all_files('*.pye', os.environ['PATH']).next( )

To print all such files, one per line:

for match in all_files('*.pye', os.environ['PATH']):     print match

To print them all at once, as a list:

print list(all_files('*.pye', os.environ['PATH']))

I have also wrapped around this all_files function a main script to show all of the files with a given name along my PATH. Thus I can see not only which one will execute for that name (the first one), but also which ones are "shadowed" by that first one:

if _ _name_ _ == '_ _main_ _':     import sys     if len(sys.argv) != 2 or sys.argv[1].startswith('-'):         print 'Use: %s <pattern>' % sys.argv[0]         sys.exit(1)     matches = list(all_files(sys.argv[1], os.environ['PATH']))     print '%d match:' % len(matches)     for match in matches:         print match

See Also

Recipe 2.18 for a simpler approach to find the first file with a specified name along the path; Library Reference and Python in a Nutshell docs for modules os and glob.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2004
Pages: 420

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net