Trying to enumerate all files in a certain directory (like 'find .' in Linux, or 'dir /s /b' in Windows).
I came up with the following nested list comprehension:
from os import walk
from os.path import join
root = r'c:\windows' #choose any folder here
allfiles = [join(root,f) for f in files for root,dirs,files in walk(root)]
Unfortunately, for the last expression, I'm getting:
NameError: name 'files' is not defined
Related to this question, which (although working) I can't understand the syntax of the nested list comprehension.
You need to reverse the nesting;
See the list comprehension documentation:
In other words, since you basically want the moral equivalent of:
your list comprehension should follow the same ordering.
it is: