liv2hak liv2hak - 7 months ago 31
Python Question

getting filename from python generator object

I am new to python. I am learning os.walk and yield. If I try to

below it prints a generator object. How do I print the actual file name generated.

import os, fnmatch

def locate(pattern, root=os.curdir):

for path, dirs, files in os.walk(os.paths.abspath(root)):
for filename in fnmatch.filter(files,pattern):
yield os.path.join(path,filename)

if __name__ == '__main__':
fname = locate('')

if __name__ == '__main__':
    for fname in locate('')

generators are not evaluated until needed (or iterated)

print next(locate("")," not found!")

is a way to just get the first match