Let's unpack the python locate function.

import os, fnmatch
 
def locate(pattern, root=os.curdir):
        for path, dirs, files in os.walk(os.path.abspath(root)):
                for filename in fnmatch.filter(files, pattern):
                        yield os.path.join(path, filename)

The key player in this function is os.walk, which returns the 3-tuple:  (dirpath, dirnames, filenames).  Next, there is os.path.abspath, which returns the full path name of the directory.  This is the equivalent of pwd on linux.

Let's see the locate function in action with some "debug" prints. I put this code in locate.py and made it search for all the PDF files in the current working directory.

import os, fnmatch
 
def locate(pattern, root=os.curdir):
        for path, dirs, files in os.walk(os.path.abspath(root)):
                print "path is " + path
                print "dirs contains:"
                print dirs
                print "files contains:"
                print files
                for filename in fnmatch.filter(files, pattern):
                        yield os.path.join(path, filename)
 
def main():
        for f in locate("*.pdf"):
                print "locate yield is " + f
                print
 
main()

This is what is printed after running locate.py:

path is /Users/james/locate_ex
dirs contains:
['Folder_1', 'Folder_2', 'Folder_3']
files contains:
['Dance_With_Dragons_maps.pdf', 'locate.py']
locate yield is /Users/james/locate_ex/Dance_With_Dragons_maps.pdf
 
path is /Users/james/locate_ex/Folder_1
dirs contains:
['SubDir']
files contains:
[]
path is /Users/james/locate_ex/Folder_1/SubDir
dirs contains:
[]
files contains:
['Dance_With_Dragons_maps.pdf']
locate yield is /Users/james/locate_ex/Folder_1/SubDir/Dance_With_Dragons_maps.pdf
 
path is /Users/james/locate_ex/Folder_2
dirs contains:
['SubDir']
files contains:
[]
path is /Users/james/locate_ex/Folder_2/SubDir
dirs contains:
[]
files contains:
['Dance_With_Dragons_maps.pdf']
locate yield is /Users/james/locate_ex/Folder_2/SubDir/Dance_With_Dragons_maps.pdf
 
path is /Users/james/locate_ex/Folder_3
dirs contains:
['SubDir']
files contains:
[]
path is /Users/james/locate_ex/Folder_3/SubDir
dirs contains:
[]
files contains:
['Dance_With_Dragons_maps.pdf']
locate yield is /Users/james/locate_ex/Folder_3/SubDir/Dance_With_Dragons_maps.pdf