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

user391339

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.

thebjorn

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Using Gradle, how can I ensure that a file exists at a certain location?

来自分类Dev

How to parse a text file (CSV) into haskell so I can operate on it?

来自分类Dev

In Puppet, how can I access a variable/attribute inside a defined type?

来自分类Dev

how to minify css file containing @page

来自分类Dev

How can I find out how much physical space a record (BLOB) in a sqlite database uses?

来自分类Dev

How can I use find() on a map inside a vector of maps?

来自分类Dev

How do I find the path of a data file in Dancer to use in Javascript?

来自分类Dev

How can I find most frequent combinations of numbers in a list?

来自分类Dev

Using Nokogiri I am unable to find certain nodes in a document

来自分类Dev

How can I rotate a Canvas containing an Image by 90, 180, 270 Degrees?

来自分类Dev

How can I use a type synonym in an instance declaration?

来自分类Dev

How can I find the string value of a property given it's value

来自分类Dev

How can I find executables installed by NuGet packages?

来自分类Dev

How do I print out just certain elements from a text file that has xml tags to a new text file?

来自分类Dev

How can I use sed to find the lines that start with "X", then find and replace inside this line using Perl?

来自分类Dev

How can I efficiently find the accuracy of a classifier

来自分类Dev

How can I find who pushed a tag(s) to BitBucket

来自分类Dev

How can I work with a .csv file using Java Spring Resttample?

来自分类Dev

How can I used ReadAllLines with gzipped file

来自分类Dev

How can i get from a FileInfo[] the file full name full directory including the file extension?

来自分类Dev

How can I print contents of file given filename as stdin in bash?

来自分类Dev

How can I loop through values in my json file?

来自分类Dev

How to count directories in make file

来自分类Dev

How to find out the implementation details for a certain standard C++ function?

来自分类Dev

How can I use sed to make thousands of substitutions in a file using a reference file?

来自分类Dev

Why can't I find the 'rabbitmq.config' file while I have already installed RabbitMQ?

来自分类Dev

file command + search directories and sub-directories

来自分类Dev

How can I recursively grep through several directories at once?

来自分类Dev

How can I find my browser web log file?

Related 相关文章

  1. 1

    Using Gradle, how can I ensure that a file exists at a certain location?

  2. 2

    How to parse a text file (CSV) into haskell so I can operate on it?

  3. 3

    In Puppet, how can I access a variable/attribute inside a defined type?

  4. 4

    how to minify css file containing @page

  5. 5

    How can I find out how much physical space a record (BLOB) in a sqlite database uses?

  6. 6

    How can I use find() on a map inside a vector of maps?

  7. 7

    How do I find the path of a data file in Dancer to use in Javascript?

  8. 8

    How can I find most frequent combinations of numbers in a list?

  9. 9

    Using Nokogiri I am unable to find certain nodes in a document

  10. 10

    How can I rotate a Canvas containing an Image by 90, 180, 270 Degrees?

  11. 11

    How can I use a type synonym in an instance declaration?

  12. 12

    How can I find the string value of a property given it's value

  13. 13

    How can I find executables installed by NuGet packages?

  14. 14

    How do I print out just certain elements from a text file that has xml tags to a new text file?

  15. 15

    How can I use sed to find the lines that start with "X", then find and replace inside this line using Perl?

  16. 16

    How can I efficiently find the accuracy of a classifier

  17. 17

    How can I find who pushed a tag(s) to BitBucket

  18. 18

    How can I work with a .csv file using Java Spring Resttample?

  19. 19

    How can I used ReadAllLines with gzipped file

  20. 20

    How can i get from a FileInfo[] the file full name full directory including the file extension?

  21. 21

    How can I print contents of file given filename as stdin in bash?

  22. 22

    How can I loop through values in my json file?

  23. 23

    How to count directories in make file

  24. 24

    How to find out the implementation details for a certain standard C++ function?

  25. 25

    How can I use sed to make thousands of substitutions in a file using a reference file?

  26. 26

    Why can't I find the 'rabbitmq.config' file while I have already installed RabbitMQ?

  27. 27

    file command + search directories and sub-directories

  28. 28

    How can I recursively grep through several directories at once?

  29. 29

    How can I find my browser web log file?

热门标签

归档