0

I want to make an array of subdirectories in Python. Here is an example layout and model list I would like to obtain.

                             Root
                              |
                         directories
                            /    \
                      subdir_1... subdir_n

Hence from the root, I would like to run a program that will make a list of all the subdirectories. That way if I were to write:

print(List_of_Subdirectories)

Where List_of_Subdirectories is the list of appended directories. I would obtain the output:

[subdir_1, subdir_2, ... , subdir_n]

In essence, I would like to achieve the same results as if I were to hard code every directory into a list. For example:

List_of_Subdirectories = ["subdir_1", "subdir_2", ... , "subdir_n"]

Where subdir_n denotes an arbitrary nth directory.

Unlike other posts here on stack overflow, I would like the list to contain just the directory names without tuples or paths.

segfault
  • 43
  • 1
  • 10
  • Possible duplicate of [How to get all of the immediate subdirectories in Python](http://stackoverflow.com/questions/800197/how-to-get-all-of-the-immediate-subdirectories-in-python) – Peter Wood May 14 '17 at 21:58
  • Hi @PeterWood, I looked at that post and I didn't quite get the help I wanted from it. – segfault May 14 '17 at 22:00

2 Answers2

1

If you just want the directory names, you can use os.walk to do this:

os.walk(directory) will yield a tuple for each subdirectory. The first entry in the 3-tuple is a directory name. You can wrap this in a function to simply return the list of directory names like so:

def list_paths(path):
    directories = [x[1] for x in os.walk(path)]
    non_empty_dirs = [x for x in directories if x] # filter out empty lists
    return [item for subitem in non_empty_dirs for item in subitem] # flatten the list

should give you all of the directories.

Garrett Kadillak
  • 1,026
  • 9
  • 18
  • Is there any way I can avoid getting the tuples? I would like to have a list with just the directory names. It would work the exact same way as if I were to hard code each directory name myself like L = ["dir1", "dir2", "dir3", ...etc] – segfault May 14 '17 at 22:02
  • The list comprehension given will yield a list of strings. You can wrap this in a function where the parameter given is a directory path and the list is returned? – Garrett Kadillak May 14 '17 at 22:07
  • @fwubb I've updated my answer to be a function instead :) – Garrett Kadillak May 14 '17 at 22:13
  • When I do that, it prepends the path onto the directory name. How would I be able to only work with the directory name? – segfault May 14 '17 at 22:32
  • @fwubb I've updated my answer to print _all_ directory names from a given path (including duplicates). – Garrett Kadillak May 14 '17 at 23:18
-1

If all you want is a list of the subdirectories of an specified directory, all you need is os.listdir and a filter to display only directories.

It's as simple as:

List_of_Subdirectories = list(filter(os.path.isdir, os.listdir()))
print(List_of_Subdirectories)

The return from os.listdir is a list containing the names of all the available elements in the specified directory (or . by default), directories and files. We filter only the directories using os.path.isdir. Then, as you want a list, we explicitly convert the filtered result.

You wouldn't be able to print the filtered result, but you would be able to iterate over it. The snippet below would achieve the same result as the one avobe.

directory_elements = filter(os.path.isdir, os.listdir())
List_of_Subdirectories = []
for element in directory_elements:
    List_of_Subdirectories.append(element)
print(List_of_Subdirectories)
Wilk Maia
  • 140
  • 7