Something I wrote several years ago that might help:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# catwalk - learning how to climb trees. ;-)
# Written by Kevin Cole <kjcole@gri.gallaudet.edu> 2009.07.01
#
# This little ditty does a recursive listing of the contents of
# the argument given in os.walk(), but only prints out those
# matching pattern (e.g., files ending in .asc)
import os
import re
from os.path import join, expanduser
def fetch(root, pattern):
"""Returns a list of fully qualified paths
under 'root' matching regex 'pattern'"""
targets = re.compile(pattern) # File regex
for path, dirs, files in os.walk(root):
for file in files:
if not targets.search(file): # Skip files that don't match
continue
yield join(path, file) # Like "%s/%s" % (path, file)
def main():
"""Usage: python3 catwalk.py "/starting/path" "file_extension_regex" """
import sys
tree = expanduser(sys.argv[1]) # e.g. "~/yada-yada/"
files = r"\.{0}$".format(sys.argv[2]) # e.g. "csv"
for nextfile in fetch(tree, files):
print(nextfile)
if __name__ == "__main__":
main()