title: Duplicate data when append data into file in python
body: <p>I'm doing a python scripting for error handling where if i = j then data create new result but if i != j it saves the error data into a file.However, as the error handling is in the looping function (for i in data / for j in types), the error data saved in a file is duplicated for many times as showed in the result below.</p>
  <p>Code:</p>
  <pre><code>with open(INPUT_PATH) as z:
      data = json.load(z)
      for i in data:
          if i['family']:
              if i['type']:
                  for j in types:
                      if i['type'] == j:  
                          with morpfw.request_factory(setting) as request:
                              col=request.get_collection(&quot;data&quot;)
                              
                              item = col.create({&quot;name&quot;:i[&quot;family&quot;],&quot;type&quot;:i[&quot;type&quot;]})
                      
                      else:
                          with open('report.log', 'a') as l:
                              l.write(datetime +  i['name'] + &quot;,&quot; + i['family'] + &quot;,&quot; + i['type'] + &quot;- invalid type&quot;)
          else:
              with open('report.log', 'a') as l:
                  l.write(datetime  + i['name'] + &quot;,&quot; + &quot;,&quot; + i['type'] + &quot;- No family name&quot;)
  </code></pre>
  <p>result (try to copy this result into a file to see the duplicate pattern):</p>
  <pre><code>16-Dec-2020 (04:42:14.687834)Downloader.Wowick16-Dec-2020 (04:42:14.687834)Trojan.W32.Vigorf16-Dec-2020 (04:42:14.687834)Trojan.W32.Vigorf16-Dec-2020 (04:42:14.687834)Trojan.W32.Vigorf16-Dec-2020 (04:42:14.687834)Trojan.W32.Vigorf16-Dec-2020 (04:42:14.687834)Trojan.W32.Vigorf16-Dec-2020 (04:42:14.687834)Trojan.W32.Vigorf16-Dec-2020 (04:42:14.687834)Trojan.W32.Vigorf16-Dec-2020 (04:42:14.687834)Trojan.W32.Vigorf16-Dec-2020 (04:42:14.687834)Downloader.Zirchap16-Dec-2020 (04:42:14.687834)test-malware16-Dec-2020 (04:42:14.687834)test-malware16-Dec-2020 (04:42:14.687834)test-malware16-Dec-2020 (04:42:14.687834)test-malware16-Dec-2020 (04:42:14.687834)test-malware16-Dec-2020 (04:42:14.687834)test-malware16-Dec-2020 (04:42:14.687834)test-malware16-Dec-2020 (04:42:14.687834)test-malware16-Dec-2020 (04:42:14.687834)test-malware16-Dec-2020 
  </code></pre>
  <p>How should i fix the error handling so it will not duplicate the data?</p>