2

I am trying to iterate through all my shapefiles to check if they have a coordinate system assigned or not.

The script works fine and iterates through most of my .shp files, however some files seem to have some issues due to unknown reasons and stop the iteration process. The core part of my script, where the error is located is the following:

import arcpy
from arcpy import env
import os

folder = 'C:\\myfolder'
arcpy.env.workspace = folder
fcs = arcpy.ListFeatureClasses()

for shp in fcs:
    print '\t', shp
    dsc = arcpy.Describe(shp) 
    print(arcpy.Exists(shp))
    if dsc.spatialReference is None:
        print('shape not readable: ' + folder)

    else:
        sr = dsc.spatialReference

I was able to deal with some problems using the if dsc.spatialReference is None: exception.

Currently I am getting the following error which is was not able to find a solution for. I do not necessarily need to resolve that error. I just want to skip that shapefile and continue with my script.

Runtime error Traceback (most recent call last): File "", line 18, in AttributeError: DescribeData: Method spatialReference does not exist

Any suggestions?

  • 3
    you can use python's try – xlDias Dec 10 '18 at 18:04
  • 2
    In addition to try, you can try using hasattr, see the code example here – KHibma Dec 10 '18 at 19:08
5

Here's a solution using the try statement. See the code comments for explanation:

import arcpy

folder = 'C:\\myfolder'
arcpy.env.workspace = folder
fcs = arcpy.ListFeatureClasses()

for shp in fcs:
    print '\t', shp
    dsc = arcpy.Describe(shp)
    print(arcpy.Exists(shp))
    # try statements allow you to test code and keep it from crashing
    try:
        sr = dsc.spatialReference
    # In the case that a line indented under "try:" results in an AttributeError...
    except AttributeError:
        # Do whatever you want. Here, I simply print a notification
        print('The file has no attribute named "spatialReference"')
        # "continue" will stop processing this shp and skip to the next shp in the
        #   for loop
        continue
    if sr is None:
        print('shape not readable: ' + folder)
  • Thanks. It works like it should. I just wanted to add, that the following error was resolved using if dsc.spatialReference is None:, so for others, it might be more robust to keep that line in the script. -------------- Runtime error Traceback (most recent call last): File "<string>", line 33, in <module> AttributeError: 'NoneType' object has no attribute 'name'``` – ewfun Dec 11 '18 at 14:30
  • @ewfun, that line is still there in the form of if sr is None:; it simply uses the existing variable to which dsc.spatialReference has already been assigned. – Tom Dec 11 '18 at 15:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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