title: python script ends between 2 for loops
body: <p>i'm pretty new to python and would like to copy some files from one location to another one and store some information within them inside a mysql database. I have to run the script 2 times until it works. After the first attempt it stops right after the first for-loop. Executing it a second time finishes everything. </p>
  
  <pre><code>import re, shutil, glob, os, MySQLdb
  db = MySQLdb.connect(host = "localhost",user = "root",passwd = "xxx",db = "test")
  x = db.cursor()
  
  delete = """DELETE FROM import WHERE fehler = '1'""" 
  x.execute(delete)    # clear the table
  
  dest = 'local'    # paths
  path = 'local/*.txt'    # paths
  files = glob.glob(path)    # paths
  
  for orig in glob.glob(r'/mnt/sap01x/OUT/INT/success/*.txt'):
      shutil.copy2(orig, dest)    # copy all files from one to another dir
  
  for file in files:    # starting to scan the files
      f = open(file, 'r')
      lines = f.read()
      output = re.search(r'error', lines) # regexp to search for 'error'
      if output:
          query = """INSERT INTO import (Fehler,Filename) VALUES ("1","%s")"""
              x.execute(query, file)    # insert error to db
              db.commit()
              os.remove(file)    # remove file
      else:
              os.remove(file)    # remove file
  
  db.close()
  </code></pre>
  
  <p>Can someone advise what I do wrong?</p>