title: Why file.flush() in Python function is skipping lines when called after each csv.writerow()?
body: <p>I am writing csv files from a dictionary variable using csv.writerow() and I need to see lines after I write into csv, for this purpose I am using file.flush() in the following way:</p>
  <pre><code>import csv
  
  def get_header(filetype):
      headers = {
          &quot;memory&quot;: [&quot;ts&quot;,&quot;kbmemfree&quot;,&quot;kbmemused&quot;,&quot;%memused&quot;,&quot;kbbuffers&quot;,&quot;kbcached&quot;,&quot;kbcommit&quot;,&quot;%commit&quot;,&quot;kbactive&quot;,&quot;kbinact&quot;,&quot;kbdirty&quot;,],
          &quot;cpu&quot;: [&quot;ts&quot;,&quot;CPU&quot;,&quot;%usr&quot;,&quot;%nice&quot;,&quot;%sys&quot;,&quot;%iowait&quot;,&quot;%steal&quot;,&quot;%irq&quot;,&quot;%soft&quot;,&quot;%guest&quot;,&quot;%gnice&quot;,&quot;%idle&quot;,],
      }
      return headers[filetype]
  json_objs = [{'cpu': {'%usr': 11.63, '%idle': 2384.38, '%guest': 0, 'ts': '07/17/2020 09:14:28', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 3.99, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8731252, 'kbactive': 9110256, '%commit': 0, '%memused': 8.84, 'ts': '07/17/2020 09:14:28', 'kbcached': 1038080, 'kbcommit': 0, 'kbinact': 659064}}, {'cpu': {'%usr': 12.59, '%idle': 2384.41, '%guest': 0, 'ts': '07/17/2020 09:14:40', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 3, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8731308, 'kbactive': 9110224, '%commit': 0, '%memused': 8.84, 'ts': '07/17/2020 09:14:40', 'kbcached': 1038084, 'kbcommit': 0, 'kbinact': 659064}}, {'cpu': {'%usr': 58.61, '%idle': 2325.4, '%guest': 0, 'ts': '07/17/2020 09:14:52', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 15.99, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8750984, 'kbactive': 9129868, '%commit': 0, '%memused': 8.86, 'ts': '07/17/2020 09:14:52', 'kbcached': 1038112, 'kbcommit': 0, 'kbinact': 659100}}, {'cpu': {'%usr': 43.03, '%idle': 2347.97, '%guest': 0, 'ts': '07/17/2020 09:15:04', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 9, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8731484, 'kbactive': 9110512, '%commit': 0, '%memused': 8.84, 'ts': '07/17/2020 09:15:04', 'kbcached': 1038092, 'kbcommit': 0, 'kbinact': 659064}}, {'cpu': {'%usr': 10.02, '%idle': 2386.98, '%guest': 0, 'ts': '07/17/2020 09:15:16', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 3, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8731540, 'kbactive': 9110544, '%commit': 0, '%memused': 8.84, 'ts': '07/17/2020 09:15:16', 'kbcached': 1038096, 'kbcommit': 0, 'kbinact': 659068}}, {'cpu': {'%usr': 11.38, '%idle': 2384.62, '%guest': 0, 'ts': '07/17/2020 09:15:28', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 4, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8731600, 'kbactive': 9110624, '%commit': 0, '%memused': 8.84, 'ts': '07/17/2020 09:15:28', 'kbcached': 1038100, 'kbcommit': 0, 'kbinact': 659068}}, {'cpu': {'%usr': 11.55, '%idle': 2385.45, '%guest': 0, 'ts': '07/17/2020 09:15:40', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 3, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8731656, 'kbactive': 9110692, '%commit': 0, '%memused': 8.84, 'ts': '07/17/2020 09:15:40', 'kbcached': 1038100, 'kbcommit': 0, 'kbinact': 659064}}, {'cpu': {'%usr': 62.99, '%idle': 2323, '%guest': 0, 'ts': '07/17/2020 09:15:52', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 14.01, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8754072, 'kbactive': 9132892, '%commit': 0, '%memused': 8.86, 'ts': '07/17/2020 09:15:52', 'kbcached': 1038132, 'kbcommit': 0, 'kbinact': 659100}}, {'cpu': {'%usr': 29.78, '%idle': 2364.22, '%guest': 0, 'ts': '07/17/2020 09:16:04', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 6, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8731832, 'kbactive': 9110848, '%commit': 0, '%memused': 8.84, 'ts': '07/17/2020 09:16:04', 'kbcached': 1038112, 'kbcommit': 0, 'kbinact': 659068}}, {'cpu': {'%usr': 28.47, '%idle': 2368.53, '%guest': 0, 'ts': '07/17/2020 09:16:16', '%irq': 0, 'CPU': 'all', '%steal': 0, '%gnice': 0, '%iowait': 0, '%sys': 3, '%soft': 0, '%nice': 0}, 'memory': {'kbdirty': 0, 'kbmemfree': 1, 'kbbuffers': 0, 'kbmemused': 8731892, 'kbactive': 9110920, '%commit': 0, '%memused': 8.84, 'ts': '07/17/2020 09:16:16', 'kbcached': 1038176, 'kbcommit': 0, 'kbinact': 659128}}]
  mem_filename = &quot;memory.csv&quot;
  cpu_filename = &quot;cpu.csv&quot;
  mem_file_obj = open(mem_filename, &quot;ab&quot;)
  cpu_file_obj = open(cpu_filename, &quot;ab&quot;)
  mem_writer = csv.DictWriter(mem_file_obj, fieldnames=get_header(&quot;memory&quot;))
  cpu_writer = csv.DictWriter(cpu_file_obj, fieldnames=get_header(&quot;cpu&quot;))
  for row in json_objs:
      cpu_writer.writerow(row[&quot;cpu&quot;])
      mem_writer.writerow(row[&quot;memory&quot;])
      mem_file_obj.flush()
      cpu_file_obj.flush()
  mem_file_obj.close()
  cpu_file_obj.close()
  </code></pre>
  <p>Written csv for cpu:</p>
  <pre><code>07/17/2020 09:16:16,all,28.47,0,3,0,0,0,0,0,0,2368.53
  4
  gnice,%idle
  07/17/2020 09:14:28,all,11.63,0,3.99,0,0,0,0,0,0,2384.38
  </code></pre>
  <p>This piece of code is reulting into skipping data while writing. I am not sure what is the reason for this. Can someone help? Thanks!</p>