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.
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.
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']