title: Batch conversion of .dbf to .csv in Python
body: <p>I have ~300 folders with .dbf files that I would like to convert to .csv files. </p>
  
  <p>I am using <code>os.walk</code> to find all the .dbf files and then a for loop utilizing the <code>dbfpy</code> module to convert each .dbf file to a .csv. It seems to be finding and reading the .dbf files correctly but not converting them to .csv. I believe the csv.writer code is the issue. I am not receiving any errors but the files stay as .dbf.</p>
  
  <p>My code below is based on code found <a href="http://blog.cgarrard.com/2010/08/converting-dbf-to-csv-with-python.html" rel="nofollow">here</a>.</p>
  
  <pre><code>import csv
  from dbfpy import dbf
  import os
  
  path = r"\Documents\House\DBF"
  
  
  for dirpath, dirnames, filenames in os.walk(path):
      for filename in filenames:
          if filename.endswith('.DBF'):
              in_db = dbf.Dbf(os.path.join(dirpath, filename))
              csv_fn = filename[:-4]+ ".csv"
              out_csv = csv.writer(open(csv_fn,'wb'))
  
          names = []
          for field in in_db.header.fields:
              names.append(field.name)
          out_csv.writerow(names)
  
  
          for rec in in_db:
              out_csv.writerow(rec.fieldData)
  
          in_db.close()
  </code></pre>