nested list comprehension with os.walk

后端 未结 2 728
傲寒
傲寒 2021-02-04 11:56

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:

2条回答
  •  孤独总比滥情好
    2021-02-04 12:19

    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 more for or if clauses. In this case, the elements of the new list are those that would be produced by considering each of the for or if 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.

提交回复
热议问题