Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issues in Python Class for retrieving sub-subfolders

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading