of their names and which contain the search string when match is foundits full name is appended to the results list objectalternativelywe could also simply build list of all py files and search each in for loop after the walk since we're going to code much more general solution to this type of problem in thoughwe'll let this stand for now if you want to see what' really going on in the os walk generatorcall its __next__ method (or equivalentlypass it to the next built-in functionmanually few timesjust as the for loop does automaticallyeach timeyou advance to the next subdirectory in the treegen os walk( ' :\temp\test'gen __next__((' :\\temp\\test'['parts']['random bin''spam txt''temp bin''temp txt']gen __next__((' :\\temp\\test\\parts'[]['part ''part ''part ''part ']gen __next__(traceback (most recent call last)file ""line in stopiteration the library manual documents os walk further than we will here for instanceit supports bottom-up instead of top-down walks with its optional topdown=false argumentand callers may prune tree branches by deleting names in the subdirectories lists of the yielded tuples internallythe os walk call generates filename lists at each level with the os listdir call we met earlierwhich collects both file and directory names in no particular order and returns them without their directory pathsos walk segregates this list into subdirectories and files (technicallynondirectoriesbefore yielding result also note that walk uses the very same subdirectories list it yields to callers in order to later descend into subdirectories because lists are mutable objects that can be changed in placeif your code modifies the yielded subdirectory names listit will impact what walk does next for exampledeleting directory names will prune traversal branchesand sorting the list will order the walk recursive os listdir traversals the os walk tool does the work of tree traversals for uswe simply provide loop code with task-specific logic howeverit' sometimes more flexible and hardly any more work to do the walking ourselves the following script recodes the directory listing script with manual recursive traversal function ( function that calls itself to repeat its actionsthe mylister function in example - is almost the same as lister in example - but calls os listdir to generate file paths manually and calls itself recursively to descend into subdirectories directory tools