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:
import os
subdirectories = []
for item in os.listdir():
if os.path.isdir(item):
subdirectories.append(item)
print(subdirectories)
Watch a video course
Python - The Practical Guide
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.