The os.walk function in Python is a powerful function. It generates the file names and sub-directory names in a directory tree by walking the tree. For each directory in the tree, it yields a 3-tuple (dirpath, dirnames, filenames)
.
It is not well-known that you can modify dirnames
in the body of the os.walk()
loop to manipulate the recursion!
I’ve seen programmers avoid using os.walk()
, and hack their own version of it using recursive calls to os.listdir()
, with various path manipulations in the process. It was rare that the programmer doing this was not familiar with os.walk()
. More often than not, the reason was that the programmer wanted more control over the recursion. Unfortunately, if the programmer was aware that this can be done with os.walk()
, she would probably use it and save time and sweat!
This specific feature is well documented in the Python os.walk docs. Seeing how under-used it is, I wanted to highlight it here, hoping it will serve someone out there 🙂 .