0

I have the following code that I developed to convert my .dbf zonal statistics outputs into .xls using arcpy. I am trying to run the loop using three files as a test; ppt_07223000_190001.dbf, ppt_07223000_190002.dbf and ppt_07223000_190001.dbf

import arcpy
import os
import glob
from arcpy.sa import *
from arcpy import env
import numpy as np

####################
# Set the workspace#
####################

arcpy.env.overwriteOutput=True
arcpy.env.workspace = r'D:\Prism\tdmean files\tdmean_Excel'

############################
### Search for dbf files ###
############################

dbf_filenames = sorted(glob.glob("*.dbf"))

# pieces = 10
# new_arrays = np.array_split(dbf_filenames, pieces)

for dbf in dbf_filenames:
    # print(dbf)
    basefilename = os.path.basename(dbf)
    # print(basefilename)
    CleanBaseStr = os.path.splitext(basefilename)[0]
    field_name = "Year_Month"
    field_type = "DOUBLE"

    # arcpy.management.DeleteField(dbf, field_name)
    arcpy.management.AddField(dbf, field_name, field_type, field_length = 6)

    field = ["Year_Month"]
    yearmonth = os.path.splitext(basefilename)[0][13:21]

    with arcpy.da.UpdateCursor(dbf, field) as cursor:
            try:
                for row in cursor:

                    row[0] = yearmonth
                    cursor.updateRow(row)

                    ### Execute table to excel
                    out_xls = CleanBaseStr + ".xls"
                    print(f"{out_xls} : {row}")
                    arcpy.TableToExcel_conversion(dbf, out_xls)
            except:
                continue

Ideally the output should look something like this. Excel Output

However, the issue arises in the directory. For some reason, the loop gets stuck after the first run and gets locked there.

# ppt_07223000_190001.xls : ['190001']

There is no error produced which makes it a bit more confusing. locked files

However, when changing the arcpy.env.workspace to an external workspace, the script runs as shown by this print statement, but no excel output is produced.

# ppt_07223000_190001.xls : ['190001']
# ppt_07223000_190002.xls : ['190002']
# ppt_07223000_190003.xls : ['190003']
2
  • 1
    Shouldn't the table to Excel line be outside of your update cursor loop or are you creating an excel doc for each record in each dbf table?
    – Howeitzer
    Commented Sep 13, 2022 at 22:00
  • @Howeitzer Each dbf table is only 1 row long as I am trying to extract yearly data for the same shape file. So what I am trying to do is convert each dbf table individually into a separate excel doc Commented Sep 13, 2022 at 22:10

1 Answer 1

0

The update cursor will lock the dbf file as you are inside it. One option is to just keep track of what you want to save out as and then loop through it at the end. Something like this.

    out_files = []    
    for dbf in dbf_filenames:
        .....
        with arcpy.da.UpdateCursor(dbf, field) as cursor:
                try:
                    for row in cursor:
    
                        row[0] = yearmonth
                        cursor.updateRow(row)
    
                        ### Execute table to excel
                        out_xls = CleanBaseStr + ".xls"
                        print(f"{out_xls} : {row}")
                        out_files.append([dbf, out_xls])
                except:
                    continue

    for dbf,out_xls in out_files:
        arcpy.TableToExcel_conversion(dbf, out_xls)
4
  • Thanks so much for the help, but even after doing this the .dbf file still produces a lock file. The loop gets stuck at ppt_07223000_190001.xlsx Commented Sep 14, 2022 at 16:59
  • Also, when I try to set the arcpy.env.workspace to an external workspace, I get the error OSError: "ppt_07223000_190001.dbf" does not exist Commented Sep 14, 2022 at 23:11
  • It looks like the TableToExcel does not release the lock before proceeding to the next dbf file, and you are using the same dbf file repeatedly. You could try importing time, and using time.sleep(number of seconds) in order to pause and let arcpy finish the conversion and releasing the dbf file. You could also copy your dbf file into a pandas dataframe in memory and save it out using to_xlsx.
    – dslamb
    Commented Sep 15, 2022 at 11:56
  • Thank you so much for the help with this problem, I had to change the code where I converted the .dbf files to a pandas.dataframe() and then managed to convert that output to separate excel files. It works just like I wanted it to. Commented Sep 15, 2022 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.