user391339 user391339 - 6 months ago 10
Python Question

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.

Answer

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

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