title: Python - fnmatch function that lists files in a directory keeps previous contents
body: <p>I'm trying to get all of the files in a certain directory (and its sub-directories), that are of a specific format.</p>
  
  <p>I found a code that could help me <a href="http://rosettacode.org/wiki/Walk_a_directory/Recursively#Python" rel="nofollow">here</a>, that goes as follows:</p>
  
  <pre><code>from fnmatch import fnmatch
  import os, os.path
  
  def print_fnmatches(pattern, dir, files):
      for filename in files:
      if fnmatch(filename, pattern):
          print os.path.join(dir, filename)
  
  os.path.walk('/', print_fnmatches, '*.mp3')
  </code></pre>
  
  <p>I changed it up a bit to fit my needs. I created a new module, and these are its contents:</p>
  
  <pre><code>from fnmatch import fnmatch
  import os.path
  
  filestotag = []
  
  def listoffilestotag(path):
      os.path.walk(path, fnmatches, '*.txt')
      return filestotag
  
  def fnmatches(pattern, direc, files): 
      for filename in files:
          if fnmatch(filename, pattern):
              filestotag.append(os.path.join(direc, filename)) 
  </code></pre>
  
  <p>From a different module, I could call <code>listoffilestotag()</code> and it works fine.</p>
  
  <p>However, when I call it for a second time, it seems that 'filestotag' keeps its previous contents. Why? How could I fix this? Please note that I don't entirely understand the implementation I wrote...</p>
  
  <p>Thank you!</p>