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


    # 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)


    # Select all records in layerFC


    # 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)


    # Add records with incorrect service day back to selection

    QF3 = "ServDay"
    QF4 = "Service_Day"
    WC3 = """ {} <> {} """.format(QF3, QF4)


    QF5 = "Week"
    QF6 = "Wk"
    RV3 = ""
    WC4 = """{0} <> {1} AND {1} <> '' """.format(QF5,QF6) 



    # Bring over route values rom spatial join

    with arcpy.da.UpdateCursor("layerFC",["OfficRT","Route"]) as cursor:
        for row in cursor:
            row[1] = row[0]

    # Bring over sequence values from Tower ODBC table
    SeqTable = 'S:\\Shared\\RouteSmart\\CO_161_Cleanscapes\\From_Tower\\CLS_XX_Last28Days.xlsx'


    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"
    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:
            row = (valueDi[key], key)
    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:

    # Delete temp files
  • Whats the rest of the script you have the beginning of a try but we do not see the rest of your code. – Hornbydd Nov 1 '16 at 21:18
  • Also your 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. – Hornbydd Nov 1 '16 at 21:20
  • 2
    @Hornbydd If I set the env.workspace wont a variable declaring the string value for the output name automatically take the workspace path? – geoJshaun Nov 1 '16 at 21:38
  • Yes it will go straight to workSpace. That's not it. Make sure you didn't leave open one of the layers in ArcMap between runs. You also probably want to set some of the optional parameters for the join. – J Kelly Nov 1 '16 at 21:47
  • @J Kelly I have "add results from geoprocessing to display" unchecked so the only layer in the map when the tool is run is the target used in the SPJ. The default values work when I run the tool from arctoolbox as well as when it's run in a similar stand alone script. The only things changing in this version are the output names. I'll give the optional parameters a shot though. – geoJshaun Nov 1 '16 at 22:01

After further research I discovered that deleting rows from a layer file using arcpy.DeleteFeatures_management() will also delete records from the parent featureclass simultaneously. This is why the SPJ output only has 20 features left in it. I thought that once I made a layer file (layerFC) the DeleteFeatures_management would only touch those features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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