arcpy delete_management error trapping when table de

1367
8
Jump to solution
07-21-2017 12:50 PM
SherrieKubis
Occasional Contributor

Simple code to delete a registered table.  By mistake I passed a table name that doesn't exist, but this condition must not be considered an exception because I get the print statement about it being deleted. 

import arcpy, sys
# Connection definitions
DatabaseConnectionFolder = r'C:/DatabaseConnections/sde105/'
sdecreator_sdeoltp = r'sdecreator_sdeoltp.sde/SDECREATOR.'
gdb = DatabaseConnectionFolder+sdecreator_sdeoltp
arcpy.env.workspace = DatabaseConnectionFolder+gdb

def main():
print ('TableName ', sys.argv[1] )
inTable = gdb + sys.argv[1]

try:
    arcpy.Delete_management(inTable)
    print inTable + ' deleted.'
except:
    print 'Table not deleted ' + inTable
if __name__ == '__main__':
main()

However, if I put an if statement to see if the table exists, it works.
import arcpy, sys
# Connection definitions
DatabaseConnectionFolder = r'C:/DatabaseConnections/sde105/'
sdecreator_sdeoltp = r'sdecreator_sdeoltp.sde/SDECREATOR.'
gdb = DatabaseConnectionFolder+sdecreator_sdeoltp

arcpy.env.workspace = DatabaseConnectionFolder+gdb

def main():
    print ('TableName ', sys.argv[1] )
    inTable = gdb + sys.argv[1]
    if arcpy.Exists(inTable):
        arcpy.Delete_management(inTable)
        print inTable + ' deleted.'
    else:
        print 'Table does not exist: ' + inTable

if __name__ == '__main__':
main()

What I'd really like to do is try the Delete_management, and if it is unsuccessful for any reason, trap the reason and be able to see that. 

I'm learning python and arcpy, I'm sure this must be something obvious that i'm missing.  Does anyone have an example?  

Sherrie 

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

The "#" is a place holder. It is basically a python comment that can be used in arcpy instead of None.

You might try the SetSeverityLevel tool to trap on warnings.

>>> 
... # Set the severity level to 1 (tool warnings will throw an exception)
... arcpy.SetSeverityLevel(1)
... try:
...     arcpy.Delete_management("MyDataxyz","#")
... except arcpy.ExecuteWarning:
...     print(arcpy.GetMessages(1))
... except arcpy.ExecuteError:
...     print(arcpy.GetMessages(2))
...     
WARNING 000110: MyDataxyz does not exist

>>> 

View solution in original post

8 Replies
RandyBurton
MVP Alum

Your first section of code in the def_main() function appears not to be indented properly.  That may be the an issue.  Also, when posting code, it helps to use the syntax highlighter.  See: Code Formatting... the basics

0 Kudos
SherrieKubis
Occasional Contributor

Sorry about the indentation representation.  I couldn't find the Code Formatting syntax highlighter to post this, I'll look into it.  I'm using PyScripter for coding, it checks indentation as well.  Going to try some other things and will update.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Is this the issue?

# wrong indentation
def main():
print ('TableName ', sys.argv[1] )
inTable = gdb + sys.argv[1]

# right indentation
def main():
    print \('TableName ', sys.argv[1] )
    inTable = gdb + sys.argv[1]
RandyBurton
MVP Alum

The arcpy.Delete_management tool presents a warning message 000110 <item> does not exist and not an error which would stop execution.  The tool then continues with a succeeded message.  You may need to look at a log file to see the warnings, or use your second option to check for existence of item before attempting a delete. See GetMessage and the related help files to access these messages.

>>> arcpy.Delete_management("MyDataxyz","#")
<Result 'true'>
>>> for i in range(0,arcpy.GetMessageCount()):
...     print arcpy.GetMessage(i)
...     
Executing: Delete MyDataxyz #
Start Time: Fri Jul 21 15:12:42 2017
WARNING 000110: MyDataxyz does not exist
Succeeded at Fri Jul 21 15:12:42 2017 (Elapsed Time: 0.00 seconds)
>>> ‍‍‍‍‍‍‍‍‍‍
0 Kudos
SherrieKubis
Occasional Contributor

Thanks Randy. 

That indeed does work.  Most of the examples that I see, especially in ArcGIS Help, uses a try block.  I was assuming that if an exception was received inside of the try:, it would cascade into the Except:, but it doesn't.  The error checking has to be done inside of the try and handled there, which kind of makes Try and Except not what I want to use. In my example below, the exception returns on the first statement in the try block, but in the except block, it returns success. So why use try and except, perhaps not what I need in this case.  I'll rework to use conditional statements instead.  What does the "#" represent?  

def DeleteNonspatialTables(inTable):
    print ('TableName ', inTable)
    FullTable = gdb + sys.argv[1]
    print ('Full Table ', FullTable)

    try:
        arcpy.Delete_management(FullTable,"#")
        for i in range(0,arcpy.GetMessageCount()):
            print arcpy.GetMessage(i)
    except arcpy.ExecuteError:
        for i in range(0,arcpy.GetMessageCount()):
            print arcpy.GetMessage(i)

if __name__ == '__main__':
    DeleteNonspatialTables(sys.argv[1])
RandyBurton
MVP Alum

The "#" is a place holder. It is basically a python comment that can be used in arcpy instead of None.

You might try the SetSeverityLevel tool to trap on warnings.

>>> 
... # Set the severity level to 1 (tool warnings will throw an exception)
... arcpy.SetSeverityLevel(1)
... try:
...     arcpy.Delete_management("MyDataxyz","#")
... except arcpy.ExecuteWarning:
...     print(arcpy.GetMessages(1))
... except arcpy.ExecuteError:
...     print(arcpy.GetMessages(2))
...     
WARNING 000110: MyDataxyz does not exist

>>> 
SherrieKubis
Occasional Contributor

Randy,

Thank you so much.  I was missing the distinction between an error and a warning, once I got that concept and then added some code around this, it's what I need.  I appreciate the help. 

Sherrie

0 Kudos
RandyBurton
MVP Alum

Glad to be of help.  It's a learning experience for me, too.