How to List all of the Directories of a Directory in Python



Python


In this article, we show how to list all of the directories of a directory in Python.

This way, you can know all of the directories that exist in a directory (these can be also called subdirectories of a directory).

To show all of the directories in a directory, the code to do so is, os.listdir(pathway).

So, for example, to show all of the directories in the "C:\\Users", the code to do so is shown below.



First, we must import the os module.

After this, we must the listdir() function to list all of the directories.

The parameter we specify into the listdir() function is the directory that we want to find all the subdirectories for.

After running the code shown above, we got the following output shown below.



What you see listed are all the directories of the "Users" directory. This includes 'All Users', 'David', etc.

So, with this simple code, we can find all of the (sub)directories of a directory in Python.


Related Resources



HTML Comment Box is loading comments...