title: Python - How to read multiple files, process and write to multiple files
body: <p>I am trying to build a Source code Plagiarism detection tool in Python. I am trying to take input from multiple python program files present in an input directory, carry out some processing stuff and write the output to multiple files.</p>
  
  <p>Here's what I'm trying to do in my code:</p>
  
  <p>I have 2 files:</p>
  
  <ol>
  <li><strong>main.py</strong>, where I'm opening multiple files and calling my class &amp; methods</li>
  <li><strong>pyscp.py</strong> from package <strong>SoftPlag</strong>, where the code is written for the processing of the input files.</li>
  </ol>
  
  <h2><strong>main.py</strong></h2>
  
  <pre><code>from SoftPlag import *
  import os
  
  inputdir = "/path/to/input/directory"
  outputdir = "/path/to/output/directory"
  filelist = os.listdir(inputdir)
  
  if __name__ == '__main__':   
      for i in filelist:
          with open(inputdir + i, 'r') as f:
              fin = f.read()
              if i.endswith(".py"):
                  scp=Pyscp()    
                  scp.pscpp(fin)
                  f.flush()       
              f.close() 
  </code></pre>
  
  <h2><strong>pyscp.py</strong></h2>
  
  <blockquote>
    <p>This is the file where the processing stuff is. After each file is
    processed, the corresponding output is to be written to a file in its
    output directory</p>
  </blockquote>
  
  <pre><code>import re
  import keyword
  import os
  
  inputdir = "/path/to/input/directory"
  outputdir = "/path/to/output/directory"
  filelist = os.listdir(inputdir)
  frequency={}
  class Pyscp():
  #Python source-code processing class
      def pscpp(self,fin):
          #remove special characters from string and convert to lower-case 
          char_string=re.sub('[^a-zA-Z._]', ' ', fin).lower()
  
          #remove single occurrences of characters 
          final_string=re.sub(r'(?:^| )\w(?:$| )', '', char_string).strip() 
  
          reservedWords={} #empty list to store the reserved keywords
          reservedWords=keyword.kwlist #reserved keywords assigned to reservedWords list
  
          for word in reservedWords:
              #checking if reserved keyword exists in string or not 
              if word in final_string:
                  #substitute reserved keywords with no spaces
                  final_string=re.sub(r'\b' + word +r'\b', '', final_string)
  
              else:
                  continue
  
          for i in filelist:
              file_output = open(os.path.join(outputdir + i +".out"), 'w')
              file_output.write(final_string)
              file_output.close()
  </code></pre>
  
  <p>The problem that I'm facing is that I'm able to open multiple files using os package using a single file object and do the processing stuff on them, but it is not writing to multiple files properly .i.e. it is writing the output data of the first file multiple times and not writing different output data.</p>
  
  <p><strong>Please help!</strong></p>