2
You can use the function listdir
. This function will return all files and all directories inside the specified directory.
To check if the item is a directory, you can use the function isdir
. Note that this function needs to receive the path as a parameter complete of the item to be checked and that the return of listdir
are paths relative, then one should use the function join
to concatenate the name of the base directory with the name of the item to be checked.
from os import listdir
from os.path import isdir, join
base_dir = '/users'
diretorios = [a for a in listdir(base_dir) if isdir(join(base_dir, a))]