You are currently viewing How to Know The Names of Sub-Directories in a Folder?

How to Know The Names of Sub-Directories in a Folder?

I am sharing about the simplest way to know the names of sub-directories within a folder, taking example of flower recognition machine learning model. Most of the students of artificial intelligence who are interested in machine learning, have confusions to load data into jupyter notebook (as it could be read if someone is interested in Python Image Library – PIL or Pillow, click here ). But the main focus here is to know about the names of sub folders within a folder.

Let us assume flower recognition dataset

I have main folder named ‘flowers’ having sub directories containing different types of flowers like ‘dandelion’, ‘daisy’, ‘rose’, ‘sunflower’, ‘tulip’. The question arises “How could I know about sub-folders in this case?”, However, we could explore about sub-folders simply in file explorer in windows or linux or mac. But our target is to prepare data for machine learning in terms of labels.

There is a very simple trick, I would like to use listdir() function of os. Just follow the following code as:

  import os
  import os.path
  def list_of_folders(dir):
      folderNames = os.listdir(dir)
      for individual_folder in folderNames:             
          print([individual_folder])        
  main_folder = r'flowers'        
  list_of_folders(main_folder)

The output of above code would be as follows:

 Output:
 ['dandelion']
 ['daisy']
 ['rose']
 ['sunflower']
 ['tulip']

I would like to explain the above code as:

  • ‘os’ is the part of standard library i.e. stdlib within Python3. But it is not a built-in function in Python3 so we import os and its function os.path
  • We defined a function named list_of_folders(with one parameter i.e. directory)
  • Assigned a variable folderNames storing values (names of sub-folders) using listdir() function as os.listdir(dir)
  • For loop will pick up each individual folder name from sub-folders and print it
  • Finally tested our function list_of_folders() function providing it the name of main folder i.e. ‘flowers’, and we see the output as shown above

I would like to share a hint here. listdir() should also be use full in case if (assuming) flowers folder has three folders i.e. red_coloured_flowers, white_coloured_flowers, yellow_coloured_flowers, and these three folders have further sub folders in each of these three like ‘dandelion’, ‘daisy’, ‘rose’, ‘sunflower’, ‘tulip’.

Conclusion

We have learned how to know the names of sub-directories or sub-folders in a folder. These tricks should be known to AI learners and data science students. The output list could be used for further manipulation regarding preparation of labels data for label encoding for machine learning.

Leave a Reply