Loading data from text file from only a part of the file name
<p>I have a lot of data in different text files. Each file name contains a word that is chosen by me, but they also include a lot of "gibberish". So for example I have a text file called <code>datapoints-(my chosen name)-12iu8w9e8v09wr-140-ad92-dw9</code></p>

<p>So the <code>datapoints</code> string is in all text files, the <code>(my chosen name)</code> is what I define and know how to extract in my code, but the last bit is random. And I don't want to go and delete that part in every text file I have, that would be a bit time consuming. </p>

<p>I just want to load these text files, but I'm unsure of how to target each file without using the "gibberish" in the end. I just want to say something like: "load file that includes (my chosen name)" and then not worry about the rest.</p>

<p>this returns a list of all your files using the <a href="https://docs.python.org/2/library/glob.html" rel="nofollow">glob module</a></p>

<pre><code>import glob
your_words = ['word1', 'word2']
files = []
# find files matching 'datapoint-your words-*.txt'
for word in your_words:
    # The * is a wildcard, your words are filled in the {}. one by one
    files.extend(glob.glob('datapoint-{}-*.txt'.format(word)))

print files
</code></pre>

python