0

I have a folder structure as

>root
  > foldername1
    > subfoldername1
    > subfoldername2
    > subfoldername3
  > foldername2
    > subfoldername1
    > subfoldername2
    > subfoldername3
  > foldername3
    > subfoldername1
    > subfoldername2
    > subfoldername3

I would like to list all the file contained in the 'subfoldername3' of 'foldername2'

I approached the problem as follow, but since I have an incredible number of folders, subfolders, and files it takes forever to come to a conclusion.... is there a fastest way to do the same?

all_folders = [x[0] for x in walk(root)]
sub_folder = [s for s in all_folders if 'foldername2' in s]
matching_sub_path = [s for s in sub_folder if 'subfoldername3' in s]

matching_sub_path =
//root//foldername2//subfoldername3
gabboshow
  • 5,359
  • 12
  • 48
  • 98

1 Answers1

0

I think os.walk will list all directory recursively.

for you case try this if it suits your need.

all_folders = [x[0] for x in walk(root)]

all_files = [os.listdir(dir_path)
             for dir_pathin all_folder
                 if "subfoldername3" and "foldername2" in dir_path]
Rahul
  • 10,830
  • 4
  • 53
  • 88