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"  



#List workspaces in root directory  
workspaces=arcpy.ListWorkspaces ("*","Folder")  
for workspace in workspaces:  
    print workspace  

#Find workspaces named Drawings  
    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")  
            for dirpath, dirnames, filenames in arcpy.da.Walk(workspaceNext,topdown=True, datatype='FeatureClass',onerror=None,type='Polyline'):  
                    print dirpath  
                    for filename in filenames:  


                            desc = arcpy.Describe(dirpath)  
                            print filename  
                            print featureTest  
                            print desc.extent.XMin  

                        except arcpy.ExecuteError:  
                            arcpy.AddError("Non-tool error occurred")  
                except OSError as e:  
                    print e.errno  
                    print e.filename  
                    print e.strerror  

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

        except OSError as e:  
            print e.errno  
            print e.filename  
            print e.strerror  
        except Exception as e:  
            print "An error has occurred"  
            print e  
  • did you try pass in the try..except block to see if it will keep on marching, rather than worrying about error types for now?
    – user681
    Aug 6, 2014 at 18:31
  • Dan, I have tried pass in the try..except block. I just tried it again and, unfortunately, the walk still doesn't keep marching.
    – JRam
    Aug 6, 2014 at 18:47
  • How are the DWG polyline files corrupted?
    – Aaron
    Aug 6, 2014 at 19:05
  • You could try Pythons native os.walk instead of arcpy.da.walk to find all Dwg files.
    – klewis
    Aug 6, 2014 at 19:12
  • 1
    Please post on the Esri forums or contact Esri support. This sounds like something that needs to be fixed. Aug 6, 2014 at 20:15

2 Answers 2


Below is the code that I used to finally solve my issue. Multiprocessing and os.walk allowed me to iterate all the DWGs within the directories.Thank you everyone for your help!

import os, arcpy, multiprocessing, csv, sys

#Get spatial extent of dwg files and spatial reference
def GetExtent(ds):

    with open(outputCSV, 'a') as csvfile:
       csvwriter = csv.writer(csvfile)

#Find all .dwg files in directories
def main(workspace):
    arcpy.env.workspace = workspace
    dataset1 = arcpy.ListDatasets("*.dwg", "Feature")
    ds_list=[os.path.join(workspace,dataset) for dataset in dataset1]
    pool = multiprocessing.Pool()

#iterate directories
arcpy.env.workspace = "F:/Metro Design/_Rev 8, jobs"
workspaces = arcpy.ListWorkspaces ("*","Folder")
for workspace in workspaces:
    arcpy.env.workspace = workspace
    workspaces = arcpy.ListWorkspaces ("*rawing*","Folder")
    for workspace2 in workspaces:
        for dirname, dirnames, filenames in os.walk(workspace2):
            for subdirname in dirnames:
                WorkspacePass = os.path.join(dirname, subdirname)
                if __name__=='__main__':

I suggest to use function, something like

def Get_Xmin(desc):
  return desc.extent.XMin
  return -1

instead of continue

  • Thank you Felix. Unfortunately using the function stalls as well. ESRI support has suggested trying to use threading to open a new thread to continue if the walk is stalled for a set amount of time.
    – JRam
    Aug 12, 2014 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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