8

I hate to bug you all with the same problem over and over again but I run into a new problem everytime I make a slight change to the code. All I did to the code below was to replace field names to match the original feature class. Now it doesn't work. It gave me

<type 'exceptions.RuntimeError'>: ERROR 999999: Error executing function. Failed to execute (Script).

I tried adding Try/Except to the code which didn't give me any error messages but didn't give me any results either.

I'm not sure what's going on? Any help to figure this out will be very much appreciated.

import arcpy, os
Try:
roadpath = arcpy.GetParameterAsText(0)
tablepath = arcpy.GetParameterAsText(1)

datapath = os.path.split(tablepath)[0]
tablename = os.path.split(tablepath)[1]
rows = arcpy.SearchCursor(roadpath,"FROMLEFT <> 0 AND TOLEFT <> 0","","STREET_NAME_ID;FROMLEFT;TOLEFT","STREET_NAME_ID A;FROMLEFT A;TOLEFT A")
arcpy.env.workspace = datapath

if arcpy.Exists(tablename):
    arcpy.DeleteRows_management(tablename)
else:
    arcpy.CreateTable_management(datapath,tablename,roadpath)
    arcpy.DeleteField_management(tablename,"SHAPE_Length")

irows = arcpy.InsertCursor(tablename)
first = True
for row in rows:
    if first:
        first = False
    else:
        GISID = row.GIS_ID
        stid = row.STREET_NAME_ID
        fl = row.FROMLEFT
        tl = row.TOLEFT
        if stid == prev_stid and fl <= prev_tl:
            irow = irows.newRow()
            irow.GIS_ID = prev_GISID
            irow.STREET_NAME_ID = prev_stid
            irow.FROMLEFT = prev_fl
            irow.TOLEFT = prev_tl
            irows.insertRow(irow)
            del irow
            irow = irows.newRow()
            irow.GIS_ID = GISID
            irow.STREET_NAME_ID = stid
            irow.FROMLEFT = fl
            irow.TOLEFT = tl
            irows.insertRow(irow)
            del irow
    prev_GISID = row.GIS_ID
    prev_stid = row.STREET_NAME_ID
    prev_fl = row.FROMLEFT
    prev_tl = row.TOLEFT
del rows, irows
except:
print arcpy.GetMessages()
5
  • Hi Sam, welcome to GIS.se :) See if putting everthing from rows = to irows inclusive in a try...except block gives a more revealing error message. It would also help if you can put a sample of the data and full script somewhere (minus.com is one place for easy public sharing). Aug 3, 2011 at 19:07
  • I fixed the code formatting in your Q. Part of the error message was being eaten by the angle brackets. FYI, slap [ctrl]-[K] when in editing mode to automatically apply code formatting to selected text. A block is indented four spaces, and inline code with back ticks: ` ` (ref) Aug 3, 2011 at 19:36
  • Regarding your latest edit, it looks like the variable defined on the second line should read tablepath instead of tablename. This just a forum typo or is it actually like that in the script? The error suggests the former, so can you provide examples of the values you are passing to both of your parameters? Also do you have an import arcpy line at the top of your script?
    – blah238
    Aug 3, 2011 at 21:37
  • There's no way for us to debug this for you -- grab PyScripter or another Python IDE and start debugging. Contrary to what matt said, if you remove the try/except, you should get a full stack trace when the program bombs, hopefully leading you to the line the error is on. You could also explicitly write the stack trace using the traceback module but probably more work than just removing the try/except. See also: blogs.esri.com/Dev/blogs/geoprocessing/archive/2008/12/01/…2D00-Error-handling-in-Python-script-tools.aspx
    – blah238
    Aug 15, 2011 at 22:06
  • The problem was the length of field name. Apparently, python doesn't like long field names (>10 characters). It seems to be working now. I'm gonna try something new tomorrow and I'm sure I'll run into new set of problems. But thank you for your suggestions. I need to learn how to debug python script. Do you know of any resources?
    – Sam
    Aug 16, 2011 at 1:45

3 Answers 3

4

The parameters that you currently have set up should not actually be "output" parameters, as they are simple string arguments to your geoprocessing methods, not true outputs.

There are two ways you can go about this:

  1. Replace your two "Output Location" and "Output Table" parameters with a single output parameter of type Table to supply the full path to the output table, and update your script logic to parse this parameter into the necessary path components using os.path.split as needed for the geoprocessing functions you are using.
  2. Change your "Output Location" and "Output Table" parameters to be input parameters (I would also rename "Output Table" to "Output Table Name" to clarify that it's just the name of the table, not the full path to it). Create a new fourth parameter of type Derived and modify your script tool's ToolValidator class to set its value after parameters 2 and 3 have been validated.

The first method is probably the easier way to go -- the ToolValidator logic is tricky to get right and difficult to debug.

EDIT: Actually there is a third option that may work for your purposes -- it is the same as method two, but instead of modifying ToolValidator, call SetParameterAsText at the end of your script to set the value of the fourth, derived parameter. I have had trouble with SetParameterAsText in the past in script tools used in ModelBuilder, but it might be fixed now.

2

I'd also look at commenting out all of the code and printing out the parameter values you are passing in. Always good to put in a bit of debugging.

It is also good practise, to check the parametsr as they come in:

# First parameter is ID
ID = long(arcpy.GetParameterAsText(0))

# parameter is optional feature class prefix 
appPrefix = arcpy.GetParameterAsText(1)
if (not appPrefix) or (appPrefix == "#") or (len(appPrefix.strip()) == 0):
    appPrefix = ""

# Optional schema owner
schemaOwner = arcpy.GetParameterAsText(2)
if (not schemaOwner) or (schemaOwner == "#") or (len(schemaOwner.strip()) == 0):
    schemaOwner = ""
else:
    schemaOwner = schemaOwner + "."

# Optional workspace - for use with ArcGIS Desktop. No default.
workspace = arcpy.GetParameterAsText(3)
if (not workspace) or (workspace == "#") or (len(workspace.strip()) == 0):
    workspace = os.path.join(sys.path[0], connFile)

Something like that, then ensure you print out the variables in order you can see them:

arcpy.AddMessage("ID : " + str(Id))

or

print "ID : " + str(Id)

Etc. You need to know the values going in aren't rubbish, as you'll always be chasing your tail.

Putting the row/cur in a try catch is good practise too.

0

I agree with blah, the direction of all your parameters should be "input". Also, you are specifying the tool parameter 3 data type, the output table, as a file object; try this as a "table" or "string".

Your Answer

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

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