Fix error in Python code

4038
17
07-31-2020 10:56 AM
KhamisBaroud
New Contributor III

I have Script to create a tool, which converts a comma-delimited text file containing coordinates to a feature class within a database, but an unexpected error appears as in the attached images, is there a workaround for that?
I used ArcMap 10.8 to execute the code!

Please Help...

Script

Error

0 Kudos
17 Replies
DanPatterson
MVP Esteemed Contributor

get rid of the 'finally' section... in fact there is no reason for your code to be in a try-except block, you will get a useful error message without it


... sort of retired...
KhamisBaroud
New Contributor III

I deleted the " finally block", and the script ran without errors, but the result "Feature Class" doesn't show inside the program"ArcMap 10.8" or the location where saved it.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In Python, the finally block of code is always executed regardless of what happens above with the try/except.  The problem you are running into is that you have defined a cursor with the try statement, but there is an exception happening before the cursor is defined.  Since the finally block always executes, it is trying to delete an object that doesn't exist, and hence you are seeing a NameError that isn't being caught by your try/except.

In general, you should not be large blocks of code within try statements because it can obscure errors that you may want to pass along to the user.   When using finally statements, don't have an object in them that is defined with the try statement.

KhamisBaroud
New Contributor III

I deleted the " finally block", and the script ran without errors, but the result "Feature Class" doesn't show inside the program"ArcMap 10.8" or the location where saved it!!

0 Kudos
DanPatterson
MVP Esteemed Contributor

did you get rid of the try-except block and keep del cur and f.close lines or did you keep the try-except and move them outside of the try-except block?


... sort of retired...
KhamisBaroud
New Contributor III

I've deleted:
finally:
      del cur
      f.close ()

........................................

The Script:

0 Kudos
DavidPike
MVP Frequent Contributor

It's likely not being created and running your except block.  You can't see the error message because you're using a print statement in a script tool.

arcpy.AddMessage(arcpy.GetMessages())

I would listen to what Dan is saying and get rid of your try/except block until things start working.  You shouldn't throw everything into a script tool straight away until you know the script works, it just makes debugging that much more difficult.

DanPatterson
MVP Esteemed Contributor

delete the" try" line and the last 3 lines

dedent the intermediate code by one level and run (ie. without the try-except block)

report any error messages that you have

If you aren't running a tool you can use print statements or "tweet" them to cover both scenarios

def tweet(msg):
    """Produce a message for both arcpy and python
    : msg - a text message
    """
    m = "{}".format(msg)
    arcpy.AddMessage(m)
    print(m)

... sort of retired...
KhamisBaroud
New Contributor III

Thank you for your response.
The code is ready to go after the following fixes:
After checking the script again, I noticed two errors in writing the capital letters and an error in the position of the index number, in addition to the last three lines where I deleted them.

The errors are apparent in the attached photo.

Error

0 Kudos