0

I stumbled upon a very simple problem today. I have a directory structure like this:

main_dir
    |_A
       |_X
       |_Y  
    |_B
       |_X
       |_Y 
    |_C

       |_X
         |_PP
         |_QQ 
       |_Y
         |_PP 

I want to recursively get all the subdirectories upto the level where X and Y are present. I don't want to include PP and QQ in that list.

I tried this:

mai_dir = Path("main_dir")
subdirs = [x for x in iter(main_dir("**/*")) if x.is_dir()] 

The problem with this approach is that it includes these results as well:

main_dir/A
main_dir/B
main_dir/C
main_dir/C/X/PP
main_dir/C/Y/QQ

I want to exclude these results. I know I can apply another for loop with if-else but I am wondering if there is a way to avoid a loop here.

enterML
  • 2,110
  • 4
  • 26
  • 38

2 Answers2

1

* matches all files and directories. If you want to match only some particular directories, say so explicitly.

mai_dir = pathlib.Path(".")
print(list(mai_dir.glob("**/[XY]/")))

If a single glob won't cut it, you can create two or more lists and merge them.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

Something like this?

import os
path = r"c:\MyDownloads"
for root,dirs,files in os.walk(path):
    if root[len(path)+1:].count(os.sep)<2:
        print(os.path.join(root))

or this:

import pathlib

path = r"c:\MyDownloads"
mai_dir = pathlib.Path(path)
for i in list(mai_dir.glob("**/")):
    if len(i.parts) < len(mai_dir.parts) + 3:
        print(i)
Xenobiologist
  • 2,091
  • 1
  • 12
  • 16