I started with some code that I got from another question about the process_ stack to generate full paths for all files in the directory tree:
import os
def recursive_file_gen(mydir):
for root, dirs, files in os.walk(mydir):
for file in files:
yield os.path.join(root, file)
I wanted to add memoization, and it seemed to me that the easiest way to achieve this is to simply do recursive_file_gen instead of a list:
def recursive_file_list(mydir):
result = []
for root, dirs, files in os.walk(mydir):
for file in files:
result.append(os.path.join(root, file))
return result
This code is fine, really. Of course, it’s not difficult to understand what is happening. But combining a list using sequential operations is appendnot entirely Pythonic. My assumption is that it is better to use Python syntax, perhaps through an understanding of the list, and studying it will be edifying.
Of course, I could write:
def recursive_file_list(mydir):
return list(recursive_file_gen(mydir))
. , - .