AnsweredAssumed Answered

arcpy delete_management error trapping when table de

Question asked by sherriekubis on Jul 21, 2017
Latest reply on Jul 24, 2017 by rvburton

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 

Outcomes