Need help with script flow control

606
2
08-31-2011 02:15 PM
MattFrancis
New Contributor III
Hey All,

I am running a script that will analyze feature classes, feature datasets, and tables in a workspace.  If I encounter a workspace that has no feature datasets (for example) the logic moves into the except statement and doesn't execute analyze on tables.  Instead of failling, I would like to see a warning in the output, but continue stepping through the try: block.  Any help is greatly appreciated!

# Analyze
try:
    env.workspace = dbconnection
    fclist = arcpy.ListFeatureClasses() #  <-- executes as expected
    for fc in fclist:
        arcpy.Analyze_management(fc,'BUSINESS;FEATURE;ADDS;DELETES')
        output.write("  " + fc + " has been analyzed\n")        

    fdlist = arcpy.ListFeatureDatasets() #   <-- FAIL!!!
    for fd in fdlist:
        arcpy.Analyze_management(fd,'BUSINESS;FEATURE;ADDS;DELETES')
        output.write("  " + fd + " has been analyzed\n")

    tablist = arcpy.ListTables()  #  <-- Doesn't execute
    for tab in tablist:
        arcpy.Analyze_management(tab,'BUSINESS')
        output.write("  " + tab + " has been analyzed\n")
    output.write("  arcpy.analyze finished successfully\n")
    
except:
    output.write("!!arcpy.analyze didn't work right\n")
    err_list = str(sys.exc_info())
    for err in err_list.split('\\n'):
        output.write("  " + err + "\n")
Tags (2)
0 Kudos
2 Replies
StacyRendall1
Occasional Contributor III
You should be able to just do a sub try...except statement around the thing that fails, like so:
# Analyze
try:
    env.workspace = dbconnection
    fclist = arcpy.ListFeatureClasses() #  <-- executes as expected
    for fc in fclist:
        arcpy.Analyze_management(fc,'BUSINESS;FEATURE;ADDS;DELETES')
        output.write("  " + fc + " has been analyzed\n")       

    try:
        fdlist = arcpy.ListFeatureDatasets() #   <-- FAIL!!!
    except: # if you know the error, put it there, i.e. except ExecuteError:
        fdlist = None
    if fdlist is not None:
        for fd in fdlist:
            arcpy.Analyze_management(fd,'BUSINESS;FEATURE;ADDS;DELETES')
            output.write("  " + fd + " has been analyzed\n")

    tablist = arcpy.ListTables()  #  <-- Doesn't execute
    for tab in tablist:
        arcpy.Analyze_management(tab,'BUSINESS')
        output.write("  " + tab + " has been analyzed\n")
    output.write("  arcpy.analyze finished successfully\n")
   
except:
    output.write("!!arcpy.analyze didn't work right\n")
    err_list = str(sys.exc_info())
    for err in err_list.split('\\n'):
        output.write("  " + err + "\n")


The if statement stops it running the analysis if the dataset couldn't be found. You can copy this to the other ones as well, if there is any possibility your data set may not include feature classes or tables...

I think I once had Arc hang indefinitely on a script with a sub try...except, but I was doing that as well as sub functions and multiprocessing, so it may have been related to doing those as well.

Let me know how you get on, as there are plenty of other ways to do this (if it does have a problem).
0 Kudos
MattFrancis
New Contributor III
Thanks, Stacy
That was the conclusion I had come to as well.  Exceptions are there for a reason--to break out of a block of code that's running inconsistantly. 

Thanks again for your quick reply!
Matt
0 Kudos