Recursing through directories

In some cases, it is necessary to iterate recursively through the main directory to discover new directories. In this example, we see how we can browse a directory recursively and retrieve the names of all files within that directory:

import os # you can change the "/" to a directory of your choice for file in os.walk("/"):    print(file)

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.