3

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.

2
  • 1
    You could just loop over your search results and use os.path.dirname() to get the parent directories. Commented May 31, 2016 at 18:06
  • thanks! that worked. Commented May 31, 2016 at 18:12

1 Answer 1

4

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.