I have a list of a part of a filename, for each one I want to go through the files in a directory that matches that part and return the filename
<p>So, let's say I have a directory with a bunch of filenames. 
for example:</p>

<p>Scalar Product or Dot Product (Hindi)-fodZTqRhC24.m4a<br>
AP Physics C - Dot Product-Wvhn_lVPiw0.m4a<br>
An Introduction to the Dot Product-X5DifJW0zek.m4a   </p>

<p>Now let's say I have a list, of only the keys, which are at the end of the file names:</p>

<p><code>['fodZTqRhC24', 'Wvhn_lVPiw0, 'X5DifJW0zek']</code></p>

<p>How can I iterate through my list to go into that directory and search for a file name containing that key, and then return me the filename? </p>

<p>Any help is greatly appreciated!</p>

<p>Here's a way to do it that allows for a selection of weather to match based on placement of text.</p>

<pre><code>import os
def scan(dir, match_key, bias=2):
    '''
        :0 startswith
        :1 contains
        :2 endswith
        '''

    matches = []
    if not isinstance(match_key, (tuple, list)):
        match_key = [match_key]

    if os.path.exists(dir):
        for file in os.listdir(dir):
            for match in match_key:
                if file.startswith(match) and bias == 0 or file.endswith(match) and bias == 2 or match in file and bias == 1:
                    matches.append(file)
                    continue
    return matches

print scan(os.curdir, '.py'
</code></pre>

python