Search code examples
pythonglob

How can I recursively find the *directories* containing a file of a certain type?


I have a set of .bam, files scattered inside a tree of folders. Not every directory contains such a file. I know how to recursively get the path of the files themselves using glob, but not the directory containing them.

import glob2
bam_files = glob2.glob('/data2/**/*.bam')
print bam_files

The above code gives the .bam files, but I want just the folders. Wondering if there is a direct way to do this using glob without regular expressions.


Solution

  • Use a set and os.path.dirname() [https://docs.python.org/2/library/os.path.html#os.path.dirname]:

    import glob2
    import os 
    bam_dirs = {os.path.dirname(p) for p in glob2.glob('/data2/**/*.bam')}
    print bam_dirs