arcpy.describe in stand alone script

1380
9
11-18-2017 11:00 PM
RandyBurton
MVP Alum

Does Describe work differently in a stand-alone script vs. a script run inside ArcMap?  More specifically, does Describe use the workspace if one is specified?  Here's the script:

import arcpy

gdb = r'C:\Path\To\Default.gdb' # path to a file geodatabase
arcpy.env.workspace = gdb

for fc in arcpy.ListFeatureClasses("*"):
        desc = arcpy.Describe(fc)
        print "{} - {}".format(fc, desc.dataType)‍‍‍‍‍‍‍‍

Here's the folder structure:

File Structure

Note that there is a folder outside the file geodatabase with the same name as a feature inside it - in this case "point_fc".

Here are the results:

# Outside ArcMap in stand-alone script:
point_fc - Folder
line_fc - FeatureClass
polygon_fc - FeatureClass

# In ArcMap's Python window:
point_fc - FeatureClass
line_fc - FeatureClass
polygon_fc - FeatureClass‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The stand-alone script picked up the folder, whereas the script when ran in ArcMap picked up the feature class.  Is this expected behavior for Describe?

Tags (1)
0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

In the middle of that link, there is a suggestion that if you aren't sure about a property you should use the 'hasattr' check. I would put both in if you aren't sure.  Also, can you rename your folder and featureclass to see if the duplicate naming is doing some 'go that one already' filtering.

I don't think datatype is sufficient in all situations so an extra check especially if duplicate names of objects appear.

Below is a suggestion from the help for checking for other attributes once a datatype is specified

if hasattr(desc, "dataType"):
    print "DataType:    " + desc.dataType
if hasattr(desc, "catalogPath"):
    print "CatalogPath: " + desc.catalogPath

so I would be inclined to check whether if has 'folder' properties as well and featureclass properties too

RandyBurton
MVP Alum

There are several ways to work around this issue.  Renaming the folder is one option.  Or this adjustment to the code will also work:

for fc in arcpy.ListFeatureClasses("*"):
        desc = arcpy.Describe(gdb+'\\'+fc) # include gdb/workspace

I was expecting Describe to use the declared workspace in a stand-alone script.  But, apparently, it starts with the folder that contains the geodatabase.  This is an example where scripts work slightly different between ArcMap's Python window and a stand-alone script.

Using hasattr to check for properties is a good suggestion.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What version of ArcGIS are you running?  I just testing your code running 10.5.1, and I cannot replicate your results.  I get the same results running in a standalone script and running in the interactive Python window.

0 Kudos
RandyBurton
MVP Alum

Version is ArcMap 10.5

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What happens when you run the following outside of ArcGIS:

import arcpy
import os

fld = r"C:\tmp" # path to folder for running test

desc_fld = arcpy.CreateFolder_management(fld, "describe") 
arcpy.CreateFolder_management(desc_fld, "point_fc")
fgdb = arcpy.CreateFileGDB_management(desc_fld, "Default")
arcpy.CreateFeatureclass_management(fgdb, "point_fc", "POINT")
arcpy.CreateFeatureclass_management(fgdb, "line_fc", "POLYLINE")
arcpy.CreateFeatureclass_management(fgdb, "polygon_fc", "POLYGON")

arcpy.env.workspace = fgdb.getOutput(0)
for fc in arcpy.ListFeatureClasses("*"):
    desc = arcpy.Describe(fc)
    print "{} - {}".format(fc, desc.dataType)

The code above recreates the folder and GDB structure you display in the original post

and the results for me are:

point_fc - FeatureClass
line_fc - FeatureClass
polygon_fc - FeatureClass
>>>
RandyBurton
MVP Alum

I did try this on a computer running ver 10.2.1, and describe gave the correct results.  I will try again with the 10.5.0 version later today.

0 Kudos
RandyBurton
MVP Alum

I am still getting the folder instead of the feature with 10.5.0.  I added a few lines to check the working directory for Python and found if I change the cwd to the arcpy workspace then describe finds the feature.  Perhaps there's a problem with the Python path/environment?

print "Arcpy: {}".format(arcpy.env.workspace)
print "Python: {}".format(os.getcwd())

for fc in arcpy.ListFeatureClasses("*"):
        desc = arcpy.Describe(fc)
        print "{} - {}".format(fc, desc.dataType)

os.chdir(arcpy.env.workspace)
print "\nNow: {}".format(os.getcwd())

for fc in arcpy.ListFeatureClasses("*"):
        desc = arcpy.Describe(fc)
        print "{} - {}".format(fc, desc.dataType)

# Output (using IDLE):

Arcpy: C:\Temp\describe\Default.gdb
Python: C:\Temp\describe
point_fc - Folder
line_fc - FeatureClass
polygon_fc - FeatureClass

Now: C:\Temp\describe\Default.gdb
point_fc - FeatureClass
line_fc - FeatureClass
polygon_fc - FeatureClass‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DanPatterson_Retired
MVP Emeritus

Did you create the same structure Joshua with the duplicates?

Randy what was in the folder just in case that has an impact as well?

0 Kudos
RandyBurton
MVP Alum

In my latest test I was using "point_fc" for a folder name, with the same name used for a feature inside the geodatabase.  I first noticed it when I moved a folder using the same name as a polygon feature that was part of a dataset into the directory with the file geodatabase.  I was using describe to look at a feature's shapeType property - which is not a property when describing a folder!

0 Kudos