e.g.
def is_json(filename):
return filename.endswith(".json")
starts = ['X:\\configs', 'X:\\todo']
d = DirectoryIterator(starts, is_json)
for jfile in d:
with open(jfile) as f:
data = json.load(f)
# [do something useful here]
I found myself doing this enough to want to hide the detail, and also avoid building big lists in memory.
First arg is a list of top directories or a single string. If you manage to put a file in there, it is just returned in the list of results. Otherwise, each iteration returns the next matching file in the tree. If you don't supply a second argument, you get all files. The second argument is a Runnable that takes the full pathname and returns True or False.