W3docs

Getting a list of all subdirectories in the current directory

Here is a code snippet in Python that gets a list of all subdirectories in the current directory:

Here is a code snippet in Python that gets a list of all subdirectories in the current directory:

Getting a list of all subdirectories in the current directory in Python

import os

subdirectories = []

for item in os.listdir():
    if os.path.isdir(item):
        subdirectories.append(item)

print(subdirectories)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

This code uses the os module to access the current directory and the os.listdir() function to get a list of all items in the directory. Then, it uses a for loop to iterate through each item and check if it is a directory using the os.path.isdir() function. If the item is a directory, it is added to the subdirectories list. Finally, the list of subdirectories is printed.