Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a handy script tool that loops through a workspace and renames and copies shapefiles to a feature dataset. However, if there is a corrupted shapefile somewhere in the workspace the script fails and stops processing. How do you handle errors such as this? Is there a way to print the error file and continue processing the next shapefile in the for loop to completion?

import arcpy
from arcpy import env

# Allow overwriting of output  
env.overwriteOutput = True

# Parameters  
env.workspace = arcpy.GetParameterAsText(0) 
state = arcpy.GetParameterAsText(1)
gdb = arcpy.GetParameterAsText(2)

# Get a list of shapefiles in folder  
fcs = arcpy.ListFeatureClasses() 

# Find the total count of shapefiles in list  
fcCount = len(fcs) 

# Set the progressor 
arcpy.SetProgressor("step", "Copying shapefiles to geodatabase...", 0,fcCount, 1) 

# For each shapefile, copy to a file geodatabase

try:
    for shp in fcs: 


        # Define name for the output points 
        fc = str(state + shp[0:9])

        # Update the progressor label for current shapefile  
        arcpy.SetProgressorLabel("Loading " + shp + "...") 

        # Copy the data  
        arcpy.CopyFeatures_management(shp, str(gdb + "\\" + fc)) 

        # Update the progressor position  
        arcpy.SetProgressorPosition()

except Exception as e:
    print "An error has occurred"
    print e

arcpy.ResetProgressor()
share|improve this question
add comment

2 Answers

up vote 12 down vote accepted

Try Googling for "python on error resume next" or similar. This returns a number of hits including this one from StackOverflow:

If you know which statements might fail, and how they might fail, then you can use exception handling to specifically clean up the problems which might occur with a particular block of statements before moving on to the next section.

1) An option may be to put a try...except block around the line you suspect will cause the problem, namely the CopyFeatures tool.

2) See also the Python reference on errors, specifically section 8.3. Once you have a reference to "e" you may be able to determine its exception type and handle it as required.

Eg this StackOverflow question contains a similar workflow to yours:

for getter in (get_random_foo, get_random_bar):
    try:
        return getter()
    except IndexError:
        continue  # Ignore the exception and try the next type.

raise IndexError, "No foos, no bars"

In your case, in place of "IndexError" you'd use whatever you determined the exception type to be for a corrupt shapefile

share|improve this answer
1  
You may also try to add your shp name to the error list in the except section. Define your ie. ErrLst = [] before FOR loop and in the except section line before CONTINUE do ErrLst.append(shp). At the end of program do-- for l in ErrLst: print >> file.txt, l . This should print your list to file. I did not test it but should work. –  Tomek Sep 13 '12 at 5:28
    
Thanks Stephen, the try/except-continue block will do the trick. –  Aaron Sep 17 '12 at 18:29
add comment

As Stephen already said you can enclose the CopyFeatures Tool in another try...except Block.

If the tool fails with a specific Shapefile you can log the Tool Message somewhere (I always print it on STDOUT and pipe the outputs to a logfile when i run the script).

What I have to add is: In the Except Block beside the Exception you also have to print the error messages the Tool itself produced. You dont get access to the Tool messages by the Exception (as it should be for sure) but from the arcpy Object by calling

arcpy.getmessages(messageCount - 1)

See http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000000m000000 how to call it and how to get the last messages which are possibly related to the specific Shapefile Error.

After logging this you simply let the script continue with the other shapefiles

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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