Recursive File Search by Regex Pattern

  • Share this:

Code introduction


This function recursively searches for files that match the specified regular expression in the given root directory.


Technology Stack : os, re

Code Type : File search

Code Difficulty : Intermediate


                
                    
import os
import re
import sys
import time

def find_files(root, pattern):
    matches = []
    for root, dirs, files in os.walk(root):
        for filename in files:
            if re.match(pattern, filename):
                matches.append(os.path.join(root, filename))
    return matches                
              
Tags: