directory we can either write recursive routine to traverse the treeor use treewalker utility built into the os module such tools can be used to searchcopycompareand otherwise process arbitrary directory trees on any platform that python runs on (and that' just about everywherethe os walk visitor to make it easy to apply an operation to all files in complete directory treepython comes with utility that scans trees for us and runs code we provide at every directory along the waythe os walk function is called with directory root name and automatically walks the entire tree at root and below operationallyos walk is generator function--at each directory in the treeit yields three-item tuplecontaining the name of the current directory as well as lists of both all the files and all the subdirectories in the current directory because it' generatorits walk is usually run by for loop (or other iteration tool)on each iterationthe walker advances to the next subdirectoryand the loop runs its code for the next level of the tree (for instanceopening and searching all the files at that levelthat description might sound complex the first time you hear itbut os walk is fairly straightforward once you get the hang of it in the followingfor examplethe loop body' code is run for each directory in the tree rooted at the current working directory along the waythe loop simply prints the directory name and all the files at the current level after prepending the directory name it' simpler in python than in english ( removed the pp subdirectory for this test to keep the output short)import os for (dirnamesubsherefilesherein os walk(')print('[dirname ']'for fname in fileshereprint(os path join(dirnamefname)handle one file \random bin \spam txt \temp bin \temp txt \parts\parts\part \parts\part \parts\part \parts\part in other wordswe've coded our own custom and easily changed recursive directory listing tool in python because this may be something we would like to tweak and reuse elsewherelet' make it permanently available in module fileas shown in example - now that we've worked out the details interactively directory tools