I have ~300 folders with .dbf files that I would like to convert to .csv files.
I am using os.walk
to find all the .dbf files and then a for loop utilizing the dbfpy
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.
My code below is based on code found here.
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()