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>Assuming you know which directory that you will be looking in, you could try something like this:</p>

<pre><code>import os

to_find = ['word 1', 'word 2']          # list containing words that you are searching for
all_files = os.listdir('/path/to/file') # creates list with files from given directory

for file in all_files:    # loops through all files in directory
    for word in to_find:  # loops through target words
        if word in file:
            print file    # prints file name if the target word is found
</code></pre>

<p>I tested this in my directory which contained these files:</p>

<pre><code>Helper_File.py
forms.py
runserver.py
static
app.py
templates
</code></pre>

<p>... and i set <code>to_find</code> to <code>['runserver', 'static']</code>...</p>

<p>and when I ran this code it returned:</p>

<pre><code>runserver.py
static
</code></pre>

<p>For future reference, you should make at least some sort of attempt at solving a problem prior to posting a question on Stackoverflow. It's not common for people to assist you like this if you can't provide proof of an attempt.</p>

python