Home > OS >  Issues in Python Class for retrieving sub-subfolders
Issues in Python Class for retrieving sub-subfolders

Time:11-16

I'm unsuccesfully trying to get a Python Function that, in form of Class, return the subfolder of a given folder.

My dataset folder is structured as follow:

    datasets 
        Actor1,
            emotion1
            emotion2
        Actor2
            emotion1
            emotion2
# setting class

class Folder:
    """Attempt to model typical folder."""
    def __init__(self, path):
        self.path = path

    def get_folder(self):
        sep_ = os.path.sep
        files = glob.glob(sep_.join([self.path, "*"]))
        dir_list = list()

        for dir_ in files:
            if os.path.isdir(dir_) == True:
                dir_list.append(dir_)
        return dir_list
        

My issue is this:

Folder(path).get_folder() returns a list holding only the element in datasets directory

Out[8]: 
[..\\datasets\\Actor1',
 '..\\datasets\\Actor2']

But my required output is:


['...\\datasets\\Actor1\\emotion1',
 '...\\datasets\\Actor1\\emotion2',
 '...\\datasets\\Actor2\\emotion2',
 '...\\datasets\\Actor2\\emotion2',]

Could you help?

CodePudding user response:

Try this:

import os
import glob


class Folder:
    """Attempt to model typical folder."""

    def __init__(self, path):
        self.path = path

    def get_folder(self):
        files = glob.glob(os.path.join(self.path, "*"))
        dir_list = []

        for f in files:
            if os.path.isdir(f):
                dir_list = dir_list   [
                    os.path.join(self.path, elt) for elt in os.listdir(f)
                ]
        return dir_list


print(Folder(".").get_folder())
  • Related