Capture corrupt files and continue script

884
2
Jump to solution
05-30-2013 08:33 PM
CPoynter
Occasional Contributor III
Hi All,

I am developing a script that walks a directory and sub-folders listing coverages, rasters and feature classes. Running successfully until it hits files that exist but not structured correctly / corrupted to be able to perform a describe function on.

I would like to capture these error files into a log file to check on them later, but have my script run to completition.

I have the following trying to capture error related files:

try:  <my code>  except arcpy.ExecuteError:              arcpy.AddError(arcpy.GetMessage(2))              f=open(r'D:\Temp\error.log', 'a')              f.write(item + "\\" + fileset)              f.close()


item represents path (e.g. "C:\Temp") and fileset is the file name (e.g. "data.shp").

Not sure what is happening as this seems to work elsewhere on the web I have seen referenced.

When I do a desc.SpatialReference.name will usually halt the script with the problem file.

I just need coding to detect problem spatial datasets, list them for later checking and move on.

Regards,

Craig
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
CPoynter
Occasional Contributor III
Hi All,

Ended up only needing one line of code to capture my error files as suggested.

try:                             val = desc.SpatialReference.name                                                      except: 


Whole code to perform my error checking:

import arcpy, sys, traceback, os from arcpy import env  env.workspace = <your workspace>  log = r'D:\Temp\error.log'  contents = []  print 'Starting'  if arcpy.Exists(log):                           arcpy.Delete_management(log)                            for dirpath, dirnames, datatypes in arcpy.da.Walk(env.workspace):         contents.append(os.path.join(dirpath))  for item in contents:     if arcpy.Exists(item):                      arcpy.env.workspace = item         featureType = ['Coverage', 'Raster', 'Feature']         for type in featureType:                 datasetList = arcpy.ListDatasets("*", type)                 for dataset in datasetList:                          data = item + "\\" + dataset                          desc = arcpy.Describe(data)                         print data + ' : ' + desc.dataType                                                  try:                             val = desc.SpatialReference.name                                                      except:                             arcpy.ExecuteError                             arcpy.AddError(arcpy.GetMessage(2))                             f=open(log, 'at')                             f.write(data + '\n')                             f.close()  ##                            tb = sys.exc_info()[2] ##                            tbinfo = traceback.format_tb(tb)[0] ##                            f=open(log, 'at') ##                            f.write(data + '\n') ##                            f.close()  print 'Done' 


Trying to describe the spatial reference would cause an error on corrupted files allowing them to be recorded for checking.

Tried using 'continue' but didn't seem to work, so leaving it out, the script runs regardless.

I was experimenting with ExecuteError and Traceback. Both will work.

(OS Windows 7, ArcGIS 10.1 SP1).

Regards,

Craig

View solution in original post

0 Kudos
2 Replies
markdenil
Occasional Contributor III
Don't wrap the whole code block in the try.
If you suspect the problem to lie in one particular part
(such as getting the SR name)
just wrap that snippit in the try...except
and end the exception with continue
That way the code has somewhere to go to continue running
(in this case, on to the next file).
0 Kudos
CPoynter
Occasional Contributor III
Hi All,

Ended up only needing one line of code to capture my error files as suggested.

try:                             val = desc.SpatialReference.name                                                      except: 


Whole code to perform my error checking:

import arcpy, sys, traceback, os from arcpy import env  env.workspace = <your workspace>  log = r'D:\Temp\error.log'  contents = []  print 'Starting'  if arcpy.Exists(log):                           arcpy.Delete_management(log)                            for dirpath, dirnames, datatypes in arcpy.da.Walk(env.workspace):         contents.append(os.path.join(dirpath))  for item in contents:     if arcpy.Exists(item):                      arcpy.env.workspace = item         featureType = ['Coverage', 'Raster', 'Feature']         for type in featureType:                 datasetList = arcpy.ListDatasets("*", type)                 for dataset in datasetList:                          data = item + "\\" + dataset                          desc = arcpy.Describe(data)                         print data + ' : ' + desc.dataType                                                  try:                             val = desc.SpatialReference.name                                                      except:                             arcpy.ExecuteError                             arcpy.AddError(arcpy.GetMessage(2))                             f=open(log, 'at')                             f.write(data + '\n')                             f.close()  ##                            tb = sys.exc_info()[2] ##                            tbinfo = traceback.format_tb(tb)[0] ##                            f=open(log, 'at') ##                            f.write(data + '\n') ##                            f.close()  print 'Done' 


Trying to describe the spatial reference would cause an error on corrupted files allowing them to be recorded for checking.

Tried using 'continue' but didn't seem to work, so leaving it out, the script runs regardless.

I was experimenting with ExecuteError and Traceback. Both will work.

(OS Windows 7, ArcGIS 10.1 SP1).

Regards,

Craig
0 Kudos