2

I am trying to describe, list, and test for existence of featureclasses in a geodatabase using the following code.

import arcpy

#Set geoprocessing environments
arcpy.env.workspace = "C:""

#Check if the environment workspace exists
if arcpy.Exists(arcpy.env.workspace):
    print "Workspace exists" + " " + "..." + " " + "getting coordinate systems" + " " + "..."
#If workspace does not exist, statement will print "does not exist"
else:
    print "Workspace does not exist"

fcList = arcpy.ListFeatureClasses("*", "POLYGON")

for fc in fcList:
        desc = arcpy.Describe(fc)
        print fc + "" + ":" + "" + str(desc.SpatialReference.name)

While the current code does work when the workspace does exist, when I try to give it a non-existing workspace I get an error message:

The issue appears to be around the line code that allows me to print the list of feature classes in the geodatabase along with there spatial reference information for fc in fcList: TypeError: "NoneType" object is not iterable

I simply want it to print statement following else: to print "the workspace does not exist", but I do not know how to actually accomplish that.

  • 1
    You should get a syntax error on line 4 (unbalanced double-quotes). Using the root of the C: drive in Windows is poor practice; try using r"C:\mydata". String math is actually quite slow, so using string.format() is the best way to format messages. – Vince Mar 27 at 18:07
  • If you change arcpy.env.workspace = "C:"" to arcpy.env.workspace = r'C:\nonExistentDir\\' your code runs and shows the message 'Workspace does not exist', but is only it what you want? – Lucas Mar 27 at 19:31
2

If you receive a correct string in workspace, your own code reorganized will do:

import arcpy

#Set geoprocessing environments
arcpy.env.workspace = r'C:\Teste\\'

#Check if the environment workspace exists
if arcpy.Exists(arcpy.env.workspace):
    fcList = arcpy.ListFeatureClasses("*", "POLYGON")
    for fc in fcList:
            desc = arcpy.Describe(fc)
            print fc + "" + ":" + "" + str(desc.SpatialReference.name)
#If workspace does not exist, statement will print "does not exist"
else:
    print "Workspace does not exist"

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.