I'm calling a python script from a custom very high level macro language script. Within the python script there is an arcpy.SpatialJoin_analysis()
command. For some reason if the output from this operation is not in the workspace geodatabase already (meaning I deleted the previous output prior to running the script), the spatial join runs but only returns 20 records out of 259. If I run it a second time, it fails to execute due to a schema lock (error 000464). I can't get the error message because the python window disappears too quickly. If I run it from the ArcMap python command window no error message comes through. I have the script set to overwrite output and my geoprocessing options allow for overwriting previous geoprocesses.
import arcpy
print" Running Python Script"
# Set up workspace and mxd
workSpace = r"C:\RS_Data\Workspace\BATCH_CO_161_imports\gisdb\layers.gdb"
arcpy.env.workspace = workSpace
arcpy.env.overwriteOutput = True
try:
# Perform a spatial join so the new routes can be assigned to the service location layer
target = r"C:\RS_Data\Workspace\BATCH_CO_161_imports\gisdb\layers.gdb\Service_Location"
join = r"C:\RS_Data\Workspace\BATCH_CO_161_imports\gisdb\layers.gdb\Suburban_Rec"
outFC = "XX_Recycle_SPJ"
arcpy.SpatialJoin_analysis(target, join, outFC)
arcpy.MakeFeatureLayer_management(outFC,"layerFC")
# Select all records in layerFC
arcpy.SelectLayerByAttribute_management("layerFC","NEW_SELECTION")
# Remove relevant commodity codes so they do not get deleted
QF1 = '\"Product_Code\"'
RV1 = '( \'R10R\', \'R10RBP\', \'R20R\', \'R20RBP\', \'R32R\', \'R32RBP\', \'R64R\', \'R64RBP\', \'R96R\', \'R96RBP\', \'R10RBY\', \'R20RBY\', \'R32RBY\', \'R64RBY\', \'R96RBY\', \'R10REC\', \'R20REC\', \'R32REC\', \'R64REC\', \'R96REC\')'
WC1 = QF1 + ' IN ' + RV1
arcpy.SelectLayerByAttribute_management("layerFC","REMOVE_FROM_SELECTION", WC1)
# Add unmatched records back to selection
QF2 = "Status"
RV2 = "M"
WC2 = """ {} <> '{}'""".format(QF2, RV2)
arcpy.SelectLayerByAttribute_management("layerFC","ADD_TO_SELECTION",WC2)
# Add records with incorrect service day back to selection
QF3 = "ServDay"
QF4 = "Service_Day"
WC3 = """ {} <> {} """.format(QF3, QF4)
arcpy.SelectLayerByAttribute_management("layerFC","ADD_TO_SELECTION",WC3)
QF5 = "Week"
QF6 = "Wk"
RV3 = ""
WC4 = """{0} <> {1} AND {1} <> '' """.format(QF5,QF6)
arcpy.SelectLayerByAttribute_management("layerFC","ADD_TO_SELECTION",WC4)
arcpy.DeleteFeatures_management("layerFC")
# Bring over route values rom spatial join
with arcpy.da.UpdateCursor("layerFC",["OfficRT","Route"]) as cursor:
for row in cursor:
row[1] = row[0]
cursor.updateRow(row)
# Bring over sequence values from Tower ODBC table
SeqTable = 'S:\\Shared\\RouteSmart\\CO_161_Cleanscapes\\From_Tower\\CLS_XX_Last28Days.xlsx'
arcpy.ExcelToTable_conversion(SeqTable,"Join_Table")
QF7 = "SVCCODE"
QV3 = "R%R%"
WC5 = """ {} LIKE '{}' """.format(QF7, QV3)
arcpy.MakeTableView_management("Join_Table", "RecSeq", WC5)
with arcpy.da.UpdateCursor("RecSeq", "CUSTID") as cursor:
for row in cursor:
row[0] = str(row[0]) + "001"
cursor.updateRow(row)
print "UPDATED ROWS"
joinFc = "RecSeq"
joinIdFld = "CUSTID"
joinValFld = "OLDSEQUENCE"
valueDi = {x[0]: x[1] for x in arcpy.da.SearchCursor(joinFc, [joinIdFld, joinValFld])}
with arcpy.da.UpdateCursor("layerFC", ["Sequence", "Address_ID"]) as cursor:
for update, key in cursor:
if not key in valueDi:
continue
row = (valueDi[key], key)
cursor.updateRow(row)
del cursor
# Export final feature class to output workspace
outputWS = r"C:\RS_Data\Workspace\BATCH_CO_161_Recycle_export\gisdb\layers.gdb"
finalFC = r"C:\RS_Data\Workspace\BATCH_CO_161_Recycle_export\gisdb\layers.gdb\XX_Recycle"
arcpy.FeatureClassToFeatureClass_conversion("layerFC", outputWS, "XX_Recycle")
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
# Delete temp files
finally:
arcpy.Delete_management("layerFC")
arcpy.Delete_management("RecSeq")
outFC
is just a name that is not a full valid path to a dataset. The path is picked up by the workspace environment you had set.