#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Batch conversion of .dbf to .csv in Python


    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.

    I am not receiving any errors but the files are not converting (staying as dbf files in path folder). Additionally, the dbf files are being emptied out (reduce to size of 0Kb) after the script is run. Note: the dbf files are stored in a subfolder of the Misc folder below.


    import csv
    from dbfpy import dbf
    import os

    path = r"C:\Users\Stephen\Documents\House\Misc"

    for dirpath, dirnames, filenames in os.walk(path):
    for filename in filenames:
    if filename.endswith( '.DBF'):
    csv_fn = filename[:- 4]+ ".CSV"
    with open(csv_fn, 'wb ') as csvfile:
    in_db = dbf.Dbf(os.path.join(dirpath, filename), new= True)
    out_csv = csv.writer(csvfile)

    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)



    Any help appreciated....
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Since it's critical to have proper whitespace to understand python code, please enclose your code in [ code ] tags so it looks like this:

    Code:
    import csv
    from dbfpy import dbf
    import os
    
    path = r"C:\Users\Stephen\Documents\House\Misc"
    
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            if filename.endswith( '.DBF'):
                csv_fn = filename[:- 4]+ ".CSV"
                with open(csv_fn, 'wb ') as csvfile:
                    in_db = dbf.Dbf(os.path.join(dirpath, filename), new= True)
                    out_csv = csv.writer(csvfile)
    
                    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)
    The way you've written this script, the .csv files should end up in the current working directory (the directory in which you're running the script). Is this the case?

    If you want them to be in the same directory structure as your .dbf files, you'll need to prepend the path to csv_fn when you create it.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    Code:
                    in_db = dbf.Dbf(os.path.join(dirpath, filename), new= True)
    I wonder what new=True means (I don't know anything about dbfPy). It can only mean a few things, like create a __new__ file in place of the old file, and none of them are good. Understand the tool before you use it.
    Last edited by dwblas; August 31st, 2013 at 11:58 AM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0
    Ahhh....thanks so much! You are both right. The New=true was emptying out my files and the conversion code was actually working fine but it was storing them in my working directory when I was expecting them in my path folder. My first program is now finished!

IMN logo majestic logo threadwatch logo seochat tools logo