title: Empty CSV file when writing lots of data
body: <p>I am currently conducting a data scraping project with Python 3 and am attempting to write the scraped data to a CSV file. My current process to do it is this:</p>
  
  <pre><code>import csv
  
  outputFile = csv.writer(open('myFilepath', 'w'))
  outputFile.writerow(['header1', 'header2'...])
  for each in data:
       scrapedData = scrap(each)
       outputFile.writerow([scrapedData.get('header1', 'header 1 NA'), ...])
  </code></pre>
  
  <p>Once this script is finished, however, the CSV file is blank. If I just run:</p>
  
  <pre><code>import csv
  
  outputFile = csv.writer(open('myFilepath', 'w'))
  outputFile.writerow(['header1', 'header2'...])
  </code></pre>
  
  <p>a CSV file is produced containing the headers:</p>
  
  <pre><code>header1,header2,..
  </code></pre>
  
  <p>If I just scrape 1 in <code>data</code>, for example:</p>
  
  <pre><code>outputFile.writerow(['header1', 'header2'...])
  scrapedData = scrap(data[0])
  outputFile.writerow([scrapedData.get('header1', 'header 1 NA'), ...])
  </code></pre>
  
  <p>a CSV file will be created including both the headers and the data for <code>data[0]</code>:</p>
  
  <pre><code>header1,header2,..
  header1 data for data[0], header1 data for data[0]
  </code></pre>
  
  <p>Why is this the case?</p>