How to get files in a directory, including all subdirectories

前端 未结 6 1473
耶瑟儿~
耶瑟儿~ 2020-12-04 12:45

I\'m trying to get a list of all log files (.log) in directory, including all subdirectories.

6条回答
  •  清歌不尽
    2020-12-04 13:30

    A single line solution using only (nested) list comprehension:

    import os
    
    path_list = [os.path.join(dirpath,filename) for dirpath, _, filenames in os.walk('.') for filename in filenames if filename.endswith('.log')]
    

提交回复
热议问题