title: How to reset dictionary on each iteration of for loop
body: <p>Below is my code to open each file in a directory and find the number of class,function, characters, and lines. When I print the output the dictionary values add up, how do I avoid this?</p>
  <pre><code>import os
  class FileAnalyzer:
      
      def __init__(self, directory: str) -&gt; None:
         
          self.directory: str = directory # NOT mandatory!
          self.files_summary: Dict[str, Dict[str, int]] = dict() 
          self.cnt=0
          self.c=0
          self.cc=0
          self.ct=0
          self.analyze_files() # summerize the python files data
  
      def analyze_files(self) -&gt; None:
          
          
          for filename in os.listdir(self.directory):
              if filename.endswith(&quot;.py&quot;):
                  with open(os.path.join(self.directory, filename), 'r') as f:
                      
                      for line in f:
                          
                          self.c+=1
                          line = line.lstrip()    
                          if line.startswith(&quot;class &quot;):
                              self.cnt+=1
                          
                          if line.startswith(&quot;def &quot;):
                              self.ct+=1
                          # Move to the start of file
                          for i in line:
                              if not line.startswith(&quot; &quot;):
                                  self.cc+=1
                          self.files_summary[filename] = {&quot;class&quot;:self.cnt,&quot;function&quot;:self.ct,&quot;line&quot;:self.c,&quot;char&quot;:self.cc}                    
                  for k,v in self.files_summary.items():
                      print (k,v)
  </code></pre>
  <p>Below are the two input files I am using:</p>
  <p>file1.py:</p>
  <pre><code>definitely not function
  
  This is def not a function def 
  </code></pre>
  <p>file2.py:</p>
  <pre><code>&quot;&quot;&quot;
      This file has:
          - 2 classes
          - 4 functions
          - 25 lines
          - 270 characters
  &quot;&quot;&quot;
  
  def func1():
      pass
  
  def func2():
      pass
  
  class Foo:
      def __init__(self):
          pass
  
  class Bar:
      def __init__(self):
          pass
  
  
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>
  <p>The output I get is:</p>
  <pre><code>file1.py {'class': 0, 'function': 0, 'line': 3, 'char': 56}
  file1.py {'class': 0, 'function': 0, 'line': 3, 'char': 56}
  file2.py {'class': 2, 'function': 4, 'line': 28, 'char': 275}
  </code></pre>
  <p>Expected output:</p>
  <pre><code>file1.py {'class': 0, 'function': 0, 'line': 3, 'char': 56}
  file2.py {'class': 2, 'function': 4, 'line': 25, 'char': 219}
  </code></pre>
  <p>I get repetition of file1 and also the counts are added when looping over the second file.</p>