Recursive File Listing Function

  • Share this:

Code introduction


This function recursively lists all files in the specified path and returns a list of file objects.


Technology Stack : Pathlib, Recursion

Code Type : Recursive file list generation

Code Difficulty : Intermediate


                
                    
def list_files_recursively(path):
    from pathlib import Path
    import os

    def _list_files(p):
        for child in p.iterdir():
            if child.is_file():
                yield child
            elif child.is_dir():
                yield from _list_files(child)

    return list(_list_files(Path(path)))