You need to cancel the nesting;
allfiles = [join(root,f) for root,dirs,files in walk(root) for f in files]
See documentation :
When a list view is provided, it consists of one expression, followed by at least one for clause and zero or more for or if clauses. In this case, the elements of the new list are those that will be created by examining each of the for or if sentences of the block, nesting from left to right, and evaluating the expression to create the list element every time the innermost block is reached.
In other words, since you basically want the moral equivalent:
allfiles = [] for root, dirs, files in walk(root): for f in files: allfiles.append(f)
Your understanding of the list must match the same order.
source share