Locate

Question

Write a program to locate a given file in the file system.

Solution

import fnmatch
import os

def locate(pattern, root=os.getcwd()):
    for path, dirs, files in os.walk(root):
        for filename in [os.path.abspath(os.path.join(path, filename)) for filename in files if fnmatch.fnmatch(filename, pattern)]:
            yield filename
Run this
Comments by Disqus