title: Add header to CSV files only once using Python - that are creating live
body: <p>So I am getting live JSON data from the stock market and creating CSV files correspondingly, for each csv file we need to add header.</p>
  <pre><code>def data_csv(self, task):
      ExchangeInstrumentName = task[&quot;Source&quot;]
      fields = [&quot;Time&quot;, &quot;LastRate&quot;, &quot;Volume&quot;, &quot;AvgRate&quot;, &quot;High&quot;, &quot;Low&quot;, &quot;Source&quot;]
      csv_file = open(f&quot;data/{ExchangeInstrumentName}.csv&quot;, &quot;a&quot;)
      csv_writer = csv.DictWriter(csv_file, delimiter=',', fieldnames=fields)
      csv_writer.writerow(task)
      # csv_writer.writeheader()
      csv_file.close()
  </code></pre>
  <p>This is the function I am using to create the CSV files. <code>fields</code> is the variable in which my headers are stored. If I use <code>writeheader()</code> - for each row the headers are getting created.</p>
  <p>I only want header at the top of the file. Please help</p>