python access selective files with glob module
Question: I have a collection of binary files which have names as so:

    
    
    d010-recomb.bin
    d011-recomb.bin
    .............
    .............
    .............
    d100-recomb.bin
    

Using the python glob module, i can access all the files in a folder and can
do further processing with these files:

    
    
    import glob
    binary = sorted(glob.glob('C:/Users/Desktop/bin/*.bin')) 
    

I can also use some criteria for the files that I want to access:

FOr example if I use the following code then I will gain access to all the
files from d010-recomb.bin to d019-recomb.bin

    
    
    binary = sorted(glob.glob('C:/Users/Desktop/bin/d01*.bin'))
    

But using this criteria I can't get access to files such as d015 to d025.

**Please** tell me what I can do to gain access to these files.


Answer: You can either filter list, using:

    
    
    def filter_path(path,l,r):
        i = int(os.path.basename(path)[1:4])
        if (i >= l) and (i <= r):
            return True
        return False
    
    result = [i for i in binary if filter_path(i,19,31)]
    

If you are 100% confident about number of elements in directory, you can:

    
    
    result = binary[19:30]
    

Or once you have data sorted, you may find the _first index_ and the _last
index_ and [[1]](http://stackoverflow.com/questions/176918/in-python-how-do-i-
find-the-index-of-an-item-given-a-list-containing-
it)[[2]](http://docs.python.org/3.2/tutorial/datastructures.html):

    
    
    l = binary.find('C:/Users/Desktop/bin/d015.bin')
    r = binary.find('C:/Users/Desktop/bin/d023.bin')
    result = binary[l:r+1]
    

