Search code examples
pythonpython-3.xpathpathlib

Getting subdirectories recursively but only up to a level


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.


Solution

  • * 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.