I am relatively new to python. I am trying to iterate through all DWG polylines in a directory with multiple (hundreds) of subdirectories. I would like the code to skip any DWG files that it cannot do something to and continue iterating.
The following code works fine, until it comes across any DWG files that are possibly corrupted. Python stalls and I cannot get it to throw an exception.
I have isolated the problem to the arcpy.da.walk. It appears that the walk function can open the corrupted DWG to find the polyline, but it cannot get out of the DWG to continue the walk.
I have tried multiple exception types. I am not sure how the onerror function call works in arcpy.da.walk,or even if this is an avenue of exploration (I cannot find any examples where onerror is anything other than "NONE").
import arcpy
import os
import sys
from arcpy import env
import pdb
##def WalkErr(onerror):
## try:
## import errno
## print errno
## if errno!=errno.ENOENT: raise
##
## elif errno == errno.EACCES:
## print filename,"cannot open"
## else:
## print errno
## print filename, "Not Found....Continuing"
## except:
## print "Something went wrong"
WorkspaceRoot="W:/GIS_Scratch"
env.workspace=WorkspaceRoot
#List workspaces in root directory
workspaces=arcpy.ListWorkspaces ("*","Folder")
for workspace in workspaces:
env.workspace=workspace
print workspace
#Find workspaces named Drawings
workspaces1=arcpy.ListWorkspaces("*rawing*","Folder")
print workspaces1
for workspaceNext in workspaces1:
arcpy.env.workspace = workspaceNext
print workspaceNext
#start stepping through code
# pdb.set_trace()
# Walk through directory to find CAD polylines (Note: "CadDrawing" datatype does not return any polylines, so had to use "FeatureClass")
try:
for dirpath, dirnames, filenames in arcpy.da.Walk(workspaceNext,topdown=True, datatype='FeatureClass',onerror=None,type='Polyline'):
try:
print dirpath
for filename in filenames:
try:
arcpy.env.workspace=filename
desc = arcpy.Describe(dirpath)
featureTest=(os.path.join(dirpath,filename))
print filename
print featureTest
print desc.extent.XMin
filename.close
except arcpy.ExecuteError:
arcpy.AddError(arcpy.GetMessages())
except:
arcpy.AddError("Non-tool error occurred")
except OSError as e:
print e.errno
print e.filename
print e.strerror
continue
except Exception as e:
print "An error has occurred"
print e
continue
except OSError as e:
print e.errno
print e.filename
print e.strerror
continue
except Exception as e:
print "An error has occurred"
print e
continue