Monday, December 1, 2008

Python recursive generator to find all the files in a directory

Just for exercise, I programed the following transverse_directory function in python to find out all the files in a directory tree in python, while trying the google treasure hunt - zip:

def transverse_directory(path):
if os.path.isdir(path):
for item in os.listdir(path):
item_path = os.path.abspath(path) + os.sep + item
for file in tranverse_directory(item_path):
yield file
else:
yield path

No comments:

Post a Comment