Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

List Sub - Directories (Excluding Hidden Ones) Non - Recursively In Python

Note that this just gives you the names. No extra paths come through.

This code :

python
from os import listdir
from os.path import isdir
from os.path import join

source_dir = "example_dir"

directories = [d for d in listdir(source_dir) if isdir(join(source_dir, d)) and d[0] != '.' ]

print(directories)

Will return this :

txt
['dir-2', 'dir-1']

Given a directory tree like :

txt
example_dir
├── .invisible-dir
│   ├── file-10.txt
│   └── file-9.txt
├── .invisible-file-1.txt
├── .invisible-file-2.txt
├── dir-1
│   ├── file-3.txt
│   └── file-4.txt
├── dir-2
│   ├── dir-3
│   │   ├── file-7.txt
│   │   └── file-8.txt
│   ├── file-5.txt
│   └── file-6.txt
├── file-1.txt
└── file-2.txt