List all subdirectories on given level

前端 未结 3 706
野性不改
野性不改 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:51

    from glob import iglob
    
    level3 = iglob('/home/backups/mysql/*/*/*')
    

    (This will skip "hidden" directories with names starting with .)

    If there may be non-directories at level 3, skip them using:

    from itertools import ifilter
    import os.path
    
    l3_dirs = ifilter(os.path.isdir, level3)
    

    In Python 3, use filter instead of ifilter.

提交回复
热议问题