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:
You need to reverse the nesting;
allfiles = [join(root,f) for root,dirs,files in walk(root) for f in files]
See the list comprehension documentation:
When a list comprehension is supplied, it consists of a single expression followed by at least one
for
clause and zero or morefor
orif
clauses. In this case, the elements of the new list are those that would be produced by considering each of thefor
orif
clauses a block, nesting from left to right, and evaluating the expression to produce a list element each time the innermost block is reached.
In other words, since you basically want the moral equivalent of:
allfiles = []
for root, dirs, files in walk(root):
for f in files:
allfiles.append(f)
your list comprehension should follow the same ordering.