959

Is there a way to return a list of all the subdirectories in the current directory in Python?

I know you can do this with files, but I need to get the list of directories instead.

2

35 Answers 35

936

Do you mean immediate subdirectories, or every directory right down the tree?

Either way, you could use os.walk to do this:

os.walk(directory)

will yield a tuple for each subdirectory. Ths first entry in the 3-tuple is a directory name, so

[x[0] for x in os.walk(directory)]

should give you all of the subdirectories, recursively.

Note that the second entry in the tuple is the list of child directories of the entry in the first position, so you could use this instead, but it's not likely to save you much.

However, you could use it just to give you the immediate child directories:

next(os.walk('.'))[1]

Or see the other solutions already posted, using os.listdir and os.path.isdir, including those at "How to get all of the immediate subdirectories in Python".

7
  • 3
    Such a clean and nice answer. Thank you. I wasn't familiar with next() and thought this link can be helpful to whoever in similar situation: stackoverflow.com/questions/1733004/python-next-function
    – Helene
    May 13, 2016 at 23:12
  • 47
    For anyone concerned about performance differences between os.walk and os.listdir+os.path.isdir solutions: I just tested on a directory with 10,000 subdirectories (with millions of files in the hierarchy below) and the performance differences are negligible. os.walk: "10 loops, best of 3: 44.6 msec per loop" and os.listdir+os.path.isdir: "10 loops, best of 3: 45.1 msec per loop"
    – kevinmicke
    Feb 28, 2017 at 19:05
  • 5
    @kevinmicke try this performance test on a network drive, I think you'll find that the performance is rather significant in that case.
    – UKMonkey
    Nov 21, 2017 at 12:53
  • @UKMonkey I'm sure you're right that a use case like that could have a significant difference.
    – kevinmicke
    Jun 2, 2018 at 17:38
  • @UKMonkey: Actually, in 3.4 and earlier they should be roughly equivalent, and in 3.5 and higher os.walk should beat os.listdir+os.path.isdir, especially on network drives. Reasons: 1) os.walk is lazy; if you do next(os.walk('.'))[1] it performs a single directory listing & categorizing by dir/non-dir, and then goes away. The cost of setting up the generator is non-zero, but it's utterly unrelated to the cost of file system access. 2) As of 3.5, os.walk is implemented via os.scandir, which doesn't require per-entry stat calls to categorize dir/non-dir (aside from symlinks)… Jul 28, 2022 at 12:17
326

You could just use glob.glob

from glob import glob
glob("/path/to/directory/*/", recursive = True)

Don't forget the trailing / after the *.

6
  • 2
    Nice. Simple. Only, it leaves the trailing / in the names Mar 21, 2018 at 16:00
  • 31
    If you cannot assume / to be the folder separator, do this: glob(os.path.join(path_to_directory, "*", "")) Mar 21, 2018 at 16:17
  • 8
    This doesn't work for subdirectories! To use glob here's the complete answer: Use a Glob() to find files recursively in Python?
    – poppie
    Oct 12, 2018 at 8:22
  • 6
    to make glob recursive you can just add the following argument recursive=True
    – JacoSolari
    Nov 12, 2019 at 12:39
  • 1
    does not work for subdirectories Jan 13, 2022 at 6:10
323

Much nicer than the above, because you don't need several os.path.join() and you will get the full path directly (if you wish), you can do this in Python 3.5 and above.

subfolders = [ f.path for f in os.scandir(folder) if f.is_dir() ]

This will give the complete path to the subdirectory. If you only want the name of the subdirectory use f.name instead of f.path

https://docs.python.org/3/library/os.html#os.scandir


Slightly OT: In case you need all subfolder recursively and/or all files recursively, have a look at this function, that is faster than os.walk & glob and will return a list of all subfolders as well as all files inside those (sub-)subfolders: https://stackoverflow.com/a/59803793/2441026

In case you want only all subfolders recursively:

def fast_scandir(dirname):
    subfolders= [f.path for f in os.scandir(dirname) if f.is_dir()]
    for dirname in list(subfolders):
        subfolders.extend(fast_scandir(dirname))
    return subfolders

Returns a list of all subfolders with their full paths. This again is faster than os.walk and a lot faster than glob.


An analysis of all functions

tl;dr:
- If you want to get all immediate subdirectories for a folder use os.scandir.
- If you want to get all subdirectories, even nested ones, use os.walk or - slightly faster - the fast_scandir function above.
- Never use os.walk for only top-level subdirectories, as it can be hundreds(!) of times slower than os.scandir.

  • If you run the code below, make sure to run it once so that your OS will have accessed the folder, discard the results and run the test, otherwise results will be screwed.
  • You might want to mix up the function calls, but I tested it, and it did not really matter.
  • All examples will give the full path to the folder. The pathlib example as a (Windows)Path object.
  • The first element of os.walk will be the base folder. So you will not get only subdirectories. You can use fu.pop(0) to remove it.
  • None of the results will use natural sorting. This means results will be sorted like this: 1, 10, 2. To get natural sorting (1, 2, 10), please have a look at https://stackoverflow.com/a/48030307/2441026


Results:

os.scandir      took   1 ms. Found dirs: 439
os.walk         took 463 ms. Found dirs: 441 -> it found the nested one + base folder.
glob.glob       took  20 ms. Found dirs: 439
pathlib.iterdir took  18 ms. Found dirs: 439
os.listdir      took  18 ms. Found dirs: 439

Tested with W7x64, Python 3.8.1.

# -*- coding: utf-8 -*-
# Python 3


import time
import os
from glob import glob
from pathlib import Path


directory = r"<insert_folder>"
RUNS = 1


def run_os_walk():
    a = time.time_ns()
    for i in range(RUNS):
        fu = [x[0] for x in os.walk(directory)]
    print(f"os.walk\t\t\ttook {(time.time_ns() - a) / 1000 / 1000 / RUNS:.0f} ms. Found dirs: {len(fu)}")


def run_glob():
    a = time.time_ns()
    for i in range(RUNS):
        fu = glob(directory + "/*/")
    print(f"glob.glob\t\ttook {(time.time_ns() - a) / 1000 / 1000 / RUNS:.0f} ms. Found dirs: {len(fu)}")


def run_pathlib_iterdir():
    a = time.time_ns()
    for i in range(RUNS):
        dirname = Path(directory)
        fu = [f for f in dirname.iterdir() if f.is_dir()]
    print(f"pathlib.iterdir\ttook {(time.time_ns() - a) / 1000 / 1000 / RUNS:.0f} ms. Found dirs: {len(fu)}")


def run_os_listdir():
    a = time.time_ns()
    for i in range(RUNS):
        dirname = Path(directory)
        fu = [os.path.join(directory, o) for o in os.listdir(directory) if os.path.isdir(os.path.join(directory, o))]
    print(f"os.listdir\t\ttook {(time.time_ns() - a) / 1000 / 1000 / RUNS:.0f} ms. Found dirs: {len(fu)}")


def run_os_scandir():
    a = time.time_ns()
    for i in range(RUNS):
        fu = [f.path for f in os.scandir(directory) if f.is_dir()]
    print(f"os.scandir\t\ttook {(time.time_ns() - a) / 1000 / 1000 / RUNS:.0f} ms.\tFound dirs: {len(fu)}")


if __name__ == '__main__':
    run_os_scandir()
    run_os_walk()
    run_glob()
    run_pathlib_iterdir()
    run_os_listdir()
4
  • 2
    it would be nice if you mention early on your question where you are substituting the different functions you profile. Regardless, impressive you spent the time doing this. Good job. I personally prefer using a single library so I liked using pathlib` as follows `[f for f in p.iterdir() if f.is_dir()]`` Jul 20, 2020 at 16:29
  • I have 50 subdirectories, each with thousands of subdirectories. I just tried running fast_scandir and it's taking over an hour. Is this normal? Is there anything I can do to speed it up?
    – Vincent
    Aug 18, 2020 at 5:12
  • for dirname in list(subfolders): that list is superfluous I believe. It wouldn't even do a copy, right?
    – A T
    Feb 9 at 3:27
  • os.scandir(folder) will fail completely if you don't have permissions or hit any other exception while it scans; this is unreliable and not a viable solution when you must return all directories except those you can't access. There doesn't seem to be a way around issue which makes os.scandir pretty unreliable and therefore largely useless IMO.
    – KyferEz
    Feb 21 at 23:00
215
import os

d = '.'
[os.path.join(d, o) for o in os.listdir(d) 
                    if os.path.isdir(os.path.join(d,o))]
5
  • 6
    note that in this approach you need to care of abspath-issues if not executed on '.' May 29, 2011 at 23:26
  • 5
    Just a heads up, if you are not using the cwd ('.'), this will not work unless you do an os.path.join on o to get the full path, otherwise isdir(0) will always return false Aug 22, 2012 at 20:32
  • 8
    It appears that the post has been updated with fixes for the two mentioned issues above.
    – cgmb
    Nov 12, 2015 at 9:14
  • 3
    To avoid calling os.path.join twice, you can first join and then filter the list using os.path.isdir: filter(os.path.isdir, [os.path.join(d, o) for o in os.listdir(d)])
    – quant_dev
    Jun 14, 2019 at 15:11
  • 2
    Using pathlib with [f for f in data_path.iterdir() if f.is_dir()] or glob is much simpler and easier to read: glob.glob("/path/to/directory/*/"). Jul 20, 2020 at 16:32
87

Python 3.4 introduced the pathlib module into the standard library, which provides an object oriented approach to handle filesystem paths:

from pathlib import Path

p = Path('./')

# All subdirectories in the current directory, not recursive.
[f for f in p.iterdir() if f.is_dir()]

To recursively list all subdirectories, path globbing can be used with the ** pattern.

# This will also include the current directory '.'
list(p.glob('**'))

Note that a single * as the glob pattern would include both files and directories non-recursively. To get only directories, a trailing / can be appended but this only works when using the glob library directly, not when using glob via pathlib:

import glob

# These three lines return both files and directories
list(p.glob('*'))
list(p.glob('*/'))
glob.glob('*')

# Whereas this returns only directories
glob.glob('*/')

So Path('./').glob('**') matches the same paths as glob.glob('**/', recursive=True).

Pathlib is also available on Python 2.7 via the pathlib2 module on PyPi.

5
  • To iterate over the list of subdirectories, here is a nice, clean syntax: for f in filter(Path.is_dir, p.iterdir()): Oct 26, 2019 at 8:00
  • Are you sure you need two stars for your glob solution? is gloab(*/) not sufficient? Regardless, fabulous answer, specially for your clean use of pathlib. It would be nice to comment if it also allows recursion, though from the title of the question that's not needed and future readers should read the docs you link. Jul 20, 2020 at 16:26
  • 2
    Thank you @CharlieParker! I updated my answer with details about recursion and using a trailing slash (including noting that trailing slashes are not necessary when using ** with pathlib's glob. Regarding, using a single asterisk, this would match files and directories non-recursively. Jul 20, 2020 at 18:14
  • glob.glob('**/', recursive=True) won't include hidden directories, but Path('./').glob('**') does
    – nos
    Jul 5, 2021 at 3:19
  • might add a sorted() at the start, so that the returned list is sorted...might or might not be useful depending on use case Apr 20, 2022 at 15:45
44

If you need a recursive solution that will find all the subdirectories in the subdirectories, use walk as proposed before.

If you only need the current directory's child directories, combine os.listdir with os.path.isdir

3
  • 8
    using pathlib is simpler: [f for f in p.iterdir() if f.is_dir()] Jul 20, 2020 at 16:26
  • 3
    @CharlieParker: this answer predated pathlib by a few years. Jul 20, 2020 at 16:53
  • Why not write the full answer in one line?
    – Jürgen K.
    Aug 19, 2021 at 9:39
35

Listing Out only directories

print("\nWe are listing out only the directories in current directory -")
directories_in_curdir = list(filter(os.path.isdir, os.listdir(os.curdir)))
print(directories_in_curdir)

Listing Out only files in current directory

files = list(filter(os.path.isfile, os.listdir(os.curdir)))
print("\nThe following are the list of all files in the current directory -")
print(files)
5
  • 9
    Did not work on mac OS. I think that the problem is that os.listdir returns only the name of the directory and not the full path but os.path.isdir only returns True if the full path is a directory.
    – denson
    Nov 22, 2016 at 17:52
  • 5
    This works outside of the current directory if you modify the line slightly: subdirs = filter(os.path.isdir, [os.path.join(dir,x) for x in os.listdir(dir)])
    – RLC
    May 7, 2019 at 17:15
  • nice job by avoiding to define lambda functions and just passing the functions directly. Jul 20, 2020 at 16:29
  • Luckily as a workaround you can just call isdir outside the filter chain on Mac OS X. Mar 11, 2022 at 6:02
  • Worked on m1 Mac as of 2023. Thank you!
    – BLimitless
    Aug 2, 2023 at 16:11
29

I prefer using filter (https://docs.python.org/2/library/functions.html#filter), but this is just a matter of taste.

d='.'
filter(lambda x: os.path.isdir(os.path.join(d, x)), os.listdir(d))
1
  • 1
    using pathlib is simpler: [f for f in p.iterdir() if f.is_dir()] Jul 20, 2020 at 16:26
25

Implemented this using python-os-walk. (http://www.pythonforbeginners.com/code-snippets-source-code/python-os-walk/)

import os

print("root prints out directories only from what you specified")
print("dirs prints out sub-directories from root")
print("files prints out all files from root and directories")
print("*" * 20)

for root, dirs, files in os.walk("/var/log"):
    print(root)
    print(dirs)
    print(files)
1
  • using pathlib is simpler: [f for f in p.iterdir() if f.is_dir()] Jul 20, 2020 at 16:29
18

You can get the list of subdirectories (and files) in Python 2.7 using os.listdir(path)

import os
os.listdir(path)  # list of subdirectories and files
3
  • 70
    This includes files too. Oct 14, 2014 at 23:20
  • 3
    The name is confusing as 'dir' does not refer to the objects forming the list but to the container directory. Please check your one-line answers, for beginners it is very tempting to select them.
    – Titou
    Sep 16, 2016 at 11:05
  • 5
    Beware of that os.listdir lists contents of directory including files.
    – guneysus
    Oct 26, 2016 at 6:56
14

Since I stumbled upon this problem using Python 3.4 and Windows UNC paths, here's a variant for this environment:

from pathlib import WindowsPath

def SubDirPath (d):
    return [f for f in d.iterdir() if f.is_dir()]

subdirs = SubDirPath(WindowsPath(r'\\file01.acme.local\home$'))
print(subdirs)

Pathlib is new in Python 3.4 and makes working with paths under different OSes much easier: https://docs.python.org/3.4/library/pathlib.html

14

Although this question is answered a long time ago. I want to recommend to use the pathlib module since this is a robust way to work on Windows and Unix OS.

So to get all paths in a specific directory including subdirectories:

from pathlib import Path
paths = list(Path('myhomefolder', 'folder').glob('**/*.txt'))

# all sorts of operations
file = paths[0]
file.name
file.stem
file.parent
file.suffix

etc.

12

Copy paste friendly in ipython:

import os
d='.'
folders = list(filter(lambda x: os.path.isdir(os.path.join(d, x)), os.listdir(d)))

Output from print(folders):

['folderA', 'folderB']
2
  • 2
    What is X in this case? Oct 28, 2018 at 5:11
  • 1
    @AbhishekParikh x is the item from the list created by os.listdir(d) because listdir will return files and folders he is using the filter command with os.path.isdir to filter any files out from the list. May 28, 2019 at 18:35
11

Thanks for the tips, guys. I ran into an issue with softlinks (infinite recursion) being returned as dirs. Softlinks? We don't want no stinkin' soft links! So...

This rendered just the dirs, not softlinks:

>>> import os
>>> inf = os.walk('.')
>>> [x[0] for x in inf]
['.', './iamadir']
2
  • 2
    What is [x[0] for x in inf] called in python so I could look it up?
    – shinzou
    Dec 23, 2017 at 13:45
  • 3
    @shinzou That's a list comprehension. Super useful. Also look up dict comprehensions.
    – KurtB
    Feb 23, 2018 at 14:39
10

Here are a couple of simple functions based on @Blair Conrad's example -

import os

def get_subdirs(dir):
    "Get a list of immediate subdirectories"
    return next(os.walk(dir))[1]

def get_subfiles(dir):
    "Get a list of immediate subfiles"
    return next(os.walk(dir))[2]
1
  • did not work. Reports "StopIteration" Jan 13, 2022 at 6:16
10

This is how I do it.

    import os
    for x in os.listdir(os.getcwd()):
        if os.path.isdir(x):
            print(x)
3
  • It does not works. I guess in x you have to provide complete path to check using isdir() Mar 20, 2020 at 9:21
  • You're probably having trouble with os.getcwd(); Essentially, what you can do is you can get the absolute path and use that instead. dir = os.path.dirname(os.path.abspath(file)) Mar 20, 2020 at 12:33
  • using os,pat.join() worked for me. Because it helped to get full path of subdirectory. Mar 25, 2020 at 17:32
9

Building upon Eli Bendersky's solution, use the following example:

import os
test_directory = <your_directory>
for child in os.listdir(test_directory):
    test_path = os.path.join(test_directory, child)
    if os.path.isdir(test_path):
        print test_path
        # Do stuff to the directory "test_path"

where <your_directory> is the path to the directory you want to traverse.

7

With full path and accounting for path being ., .., \\, ..\\..\\subfolder, etc:

import os, pprint
pprint.pprint([os.path.join(os.path.abspath(path), x[0]) \
    for x in os.walk(os.path.abspath(path))])
7

The easiest way:

from pathlib import Path
from glob import glob

current_dir = Path.cwd()
all_sub_dir_paths = glob(str(current_dir) + '/*/') # returns list of sub directory paths

all_sub_dir_names = [Path(sub_dir).name for sub_dir in all_sub_dir_paths] 
1
  • I wouldn't call that easiest, and it's needlessly complicated to mix old-style glob module usage with new-style pathlib stuff (necessitating constant conversions between str and Path objects). If you want do demonstrate pathlib stuff, stick to pathlib where possible; it's frankly prettier anyway, and clearer to boot (vs. your code relying on that trailing / to make glob.glob not return files, a behavior Path.glob doesn't replicate): all_sub_dir_names = [pth.name for pth in Path.cwd().iterdir() if pth.is_dir()] Jul 28, 2022 at 12:48
5

This answer didn't seem to exist already.

directories = [ x for x in os.listdir('.') if os.path.isdir(x) ]
2
  • 10
    This will always return an empty list if you are searching anything other than the current working directory, which is technically what the OP is looking to do, but not very reusable.
    – ochawkeye
    Feb 23, 2017 at 19:13
  • 3
    directories = [ x for x in os.listdir(localDir) if os.path.isdir(localDir+x)
    – Poonam
    Feb 15, 2018 at 6:53
5

I've had a similar question recently, and I found out that the best answer for python 3.6 (as user havlock added) is to use os.scandir. Since it seems there is no solution using it, I'll add my own. First, a non-recursive solution that lists only the subdirectories directly under the root directory.

def get_dirlist(rootdir):

    dirlist = []

    with os.scandir(rootdir) as rit:
        for entry in rit:
            if not entry.name.startswith('.') and entry.is_dir():
                dirlist.append(entry.path)

    dirlist.sort() # Optional, in case you want sorted directory names
    return dirlist

The recursive version would look like this:

def get_dirlist(rootdir):

    dirlist = []

    with os.scandir(rootdir) as rit:
        for entry in rit:
            if not entry.name.startswith('.') and entry.is_dir():
                dirlist.append(entry.path)
                dirlist += get_dirlist(entry.path)

    dirlist.sort() # Optional, in case you want sorted directory names
    return dirlist

keep in mind that entry.path wields the absolute path to the subdirectory. In case you only need the folder name, you can use entry.name instead. Refer to os.DirEntry for additional details about the entry object.

2
  • Actually, the way this is written it will not work on 3.5, only 3.6. To use on 3.5 you need to remove context manager - see stackoverflow.com/questions/41401417/…
    – havlock
    Apr 1, 2018 at 9:39
  • This is correct. I could swear I read somewhere that the context manager was implemented in 3.5, but It seems I'm wrong.
    – Alberto A
    Apr 2, 2018 at 19:58
5

using os walk

sub_folders = []
for dir, sub_dirs, files in os.walk(test_folder):
    sub_folders.extend(sub_dirs)
1
  • This will only add one sub-folder, and not lower level folders within the sub-folders. Dec 9, 2022 at 8:26
4

For anyone like me who just needed the names of the immediate folders within a directory this worked on Windows.

import os

for f in os.scandir(mypath):
    print(f.name)
1
  • 1
    The results include names of files too, at least on Debian (WSL). Jan 15, 2023 at 10:53
3

This will list all subdirectories right down the file tree.

import pathlib


def list_dir(dir):
    path = pathlib.Path(dir)
    dir = []
    try:
        for item in path.iterdir():
            if item.is_dir():
                dir.append(item)
                dir = dir + list_dir(item)
        return dir
    except FileNotFoundError:
        print('Invalid directory')

pathlib is new in version 3.4

3

Function to return a List of all subdirectories within a given file path. Will search through the entire file tree.

import os

def get_sub_directory_paths(start_directory, sub_directories):
    """
    This method iterates through all subdirectory paths of a given 
    directory to collect all directory paths.

    :param start_directory: The starting directory path.
    :param sub_directories: A List that all subdirectory paths will be 
        stored to.
    :return: A List of all sub-directory paths.
    """

    for item in os.listdir(start_directory):
        full_path = os.path.join(start_directory, item)

        if os.path.isdir(full_path):
            sub_directories.append(full_path)

            # Recursive call to search through all subdirectories.
            get_sub_directory_paths(full_path, sub_directories)

return sub_directories
2

we can get list of all the folders by using os.walk()

import os

path = os.getcwd()

pathObject = os.walk(path)

this pathObject is a object and we can get an array by

arr = [x for x in pathObject]

arr is of type [('current directory', [array of folder in current directory], [files in current directory]),('subdirectory', [array of folder in subdirectory], [files in subdirectory]) ....]

We can get list of all the subdirectory by iterating through the arr and printing the middle array

for i in arr:
   for j in i[1]:
      print(j)

This will print all the subdirectory.

To get all the files:

for i in arr:
   for j in i[2]:
      print(i[0] + "/" + j)
2

This function, with a given parent directory iterates over all its directories recursively and prints all the filenames which it founds inside. Quite useful.

import os

def printDirectoryFiles(directory):
   for filename in os.listdir(directory):  
        full_path=os.path.join(directory, filename)
        if not os.path.isdir(full_path): 
            print( full_path + "\n")


def checkFolders(directory):

    dir_list = next(os.walk(directory))[1]

    #print(dir_list)

    for dir in dir_list:           
        print(dir)
        checkFolders(directory +"/"+ dir) 

    printDirectoryFiles(directory)       

main_dir="C:/Users/S0082448/Desktop/carpeta1"

checkFolders(main_dir)


input("Press enter to exit ;")

0
1

use a filter function os.path.isdir over os.listdir() something like this filter(os.path.isdir,[os.path.join(os.path.abspath('PATH'),p) for p in os.listdir('PATH/')])

1

By joining multiple solutions from here, this is what I ended up using:

import os
import glob

def list_dirs(path):
    return [os.path.basename(x) for x in filter(
        os.path.isdir, glob.glob(os.path.join(path, '*')))]
1

Lot of nice answers out there but if you came here looking for a simple way to get list of all files or folders at once. You can take advantage of the os offered find on linux and mac which and is much faster than os.walk

import os
all_files_list = os.popen("find path/to/my_base_folder -type f").read().splitlines()
all_sub_directories_list = os.popen("find path/to/my_base_folder -type d").read().splitlines()

OR

import os

def get_files(path):
    all_files_list = os.popen(f"find {path} -type f").read().splitlines()
    return all_files_list

def get_sub_folders(path):
    all_sub_directories_list = os.popen(f"find {path} -type d").read().splitlines()
    return all_sub_directories_list
1
  • wow, you just saved my life! (figuratively) I had a few folders but millions of files, and all of the methods described above was taking forever to execute, this is so much faster.
    – Malu05
    Aug 18, 2021 at 11:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.