title: error reading files while looping through files in a directory in python2.7
body: <p>I've been googling this problem since yesterday and to no avail;</p>
  
  <p>When I loop through a number of files in one directory, and process the rows of each file within that loop, I always close, but it seems as though python is opening all the files in the same memory space, so when I loop through a file I retrieve all the records from the previously opened files, it's as if they're in a pointer array. . . .wtf?</p>
  
  <pre><code>    import os
      import sys
      import glob
      import string
      import cPickle
      path2 = './'
      columnShuffleTable = loadColumnTable('myTable') #func previously defined
      codeScrambleTable = loadScrambleTable('theirTable') #func previously defined
      pathToFiles2 = glob.glob(os.path.join(path2, '*.DAT'))
  
      for curFile in pathToFiles2:    
          _list = ['',] 
          #this is the variable with which I'm having a problem
          unscrambledCodes = file(curFile[-10:], 'r') 
          #this always yields the actual first line of the file at which I am currently at
          line = unscrambledCodes.readline() 
          _list[0] = '|' + line.strip() #stripping trailing spaces
          #the list length at this point always equates to '1', so up to here everything is great
          print "list length:", len(_list) 
          # this always reads the 2nd line of the very first file I loaded. . .wtf?
          line = unscrambledCodes.readline().strip() 
  
          while(line):
              #for unscrambledCodes [my input file] 
              print "len list: ", len(_list), "infile", unscrambledCodes 
              nextLine = unscrambledCodes.readline().strip()
  
              if not nextLine:
                  _list.append('|' + line)
                  break
              else:
                  _list.append( '|' + line[:-14] + scrambleCode(line[-12:], columnShuffleTable, codeScrambleTable))
              #end if
  
              line = nextLine
          unscrambledCodes.close()
          outfile = open(curFile[-10:-4] + '.Scrambled', 'w')
          output = '\n'.join(_list)
          outfile.write(output)
          outfile.close()
  </code></pre>
  
  <p>as requested, here are my i/o samples:</p>
  
  <p>input file1:<br/>
  AB00007737106517           COSTCLASSU275<br/>
  C000000010031932155750539976333693187714<br/>
  C000000010031932155750539976105307608239<br/></p>
  
  <p>file2:<br/>
  AB00007736638744           COSTCLASSU275<br/>
  C000000010030284907699012480608351468369<br/>
  C000000020030284907699012480751885101503<br/></p>
  
  <p>file3:<br/>
  AB00007737148207           COSTCLASSU275<br/>
  C000000010032271716759259098738354718484<br/>
  C000000020032271716759259098394986919513<br/>
  <br/>
  desired output file1:<br/>
  AB00007737148207            COSTCLASSU275<br/>
  |C000000010031932155750539976079292077121<br/>
  |C000000010031932155750539976126217711213<br/>
  <br/>
  file2:<br/>
  AB00007736638744            COSTCLASSU275<br/>
  |C000000010030284907699012480968864628712<br/>
  |C000000020030284907699012480294550195814<br/>
  <br/>
  file3:<br/>
  AB00007737106517            COSTCLASSU275<br/>
  |C000000010032271716759259098216262704445<br/>
  |C000000020032271716759259098085462231948<br/>
  <br/>
  current output file1:<br/>
  AB00007737148207            COSTCLASSU275<br/>
  |C000000010031932155750539976079292077121<br/>
  |C000000010031932155750539976126217711213<br/>
  <br/>
  file2:<br/>
  AB00007736638744            COSTCLASSU275<br/>
  |C000000010031932155750539976079292077121<br/>
  |C000000010031932155750539976126217711213<br/>
  .<br/>
  .<br/>
  .<br/>
  |C000000010030284907699012480968864628712<br/>
  |C000000020030284907699012480294550195814<br/>
  file3:<br/>
  AB00007737106517            COSTCLASSU275<br/>
  |C000000010031932155750539976079292077121<br/>
  |C000000010031932155750539976126217711213<br/>
  .<br/>
  .<br/>
  .<br/>
  |C000000010030284907699012480968864628712<br/>
  |C000000020030284907699012480294550195814<br/>
  .<br/>
  .<br/>
  .<br/>
  |C000000010032271716759259098216262704445<br/>
  |C000000020032271716759259098085462231948<br/></p>