Site icon NF AI

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:

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.

Exit mobile version