title: Convert multiple csv files to json using python
body: <p>I am trying to convert csv files in a folder to a single json file. Below code does the job, but the issue is, json file has the first csv written several times. Below is the code i tried. I guess i am going wrong with assigning the data variable. Help me fix it</p>
  
  <pre class="lang-py prettyprint-override"><code>import csv, json, os
  dir_path = 'C:/Users/USER/Desktop/output_files'
  inputfiles = [file for file in os.listdir(dir_path) if file.endswith('.csv')]
  outputfile = "data_backup1.json"
  
  for file in inputfiles:
      filepath = os.path.join(dir_path, file)
      data = {}
      with open(filepath, "r") as csvfile:       
          reader = csv.DictReader(csvfile)
          for row in reader:
              id = row['ID']
              data[id] = row
  
      with open(outputfile, "a") as jsonfile:
          jsonfile.write(json.dumps(data, indent=4))
  </code></pre>
  
  <p>Expected output: Json file needs to have each csv written only once into it.</p>