List all subdirectories on given level

前端 未结 3 712
野性不改
野性不改 2021-01-07 03:15

I have backup directory structure like this (all directories are not empty):

/home/backups/mysql/
    2012/
        12/
           15/
    2013/
        04/
         


        
3条回答
  •  我在风中等你
    2021-01-07 03:54

    You can use glob to search down a directory tree, like this:

    import os, glob
    def get_all_backup_paths(dir, level):
       pattern = dir + level * '/*'
       return [d for d in glob.glob(pattern) if os.path.isdir(d)]
    

    I included a check for directories as well, in case there might be files mixed in with the directories.

提交回复
热议问题