How to get files in a directory, including all subdirectories

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


Solution 1:

import os
import os.path

for dirpath, dirnames, filenames in os.walk("."):
    for filename in [f for f in filenames if f.endswith(".log")]:
        print os.path.join(dirpath, filename)

Solution 2:

You can also use the glob module along with os.walk.

import os
from glob import glob

files = []
start_dir = os.getcwd()
pattern   = "*.log"

for dir,_,_ in os.walk(start_dir):
    files.extend(glob(os.path.join(dir,pattern))) 

Solution 3:

Checkout Python Recursive Directory Walker. In short os.listdir() and os.walk() are your friends.

Solution 4:

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')]