Select to view content in your preferred language

ListFields for Feature Dataset from Geoprocessing Window Fails

97
3
Jump to solution
yesterday
BillMitchell
Frequent Contributor

I'm building a Python Toolbox, and am stuck pretty early on getting it to list the fields of a feature class that is part of a Feature Dataset.

I have a dataset at C:/Users/my_user/Documents/scratch/test.gdb/data/my_fc 

The code for the toolbox is below (abridged from template).

I've loaded the dataset and toolbox into my Pro project, and I'm using the geoprocessing tool, adding the dataaset via the dropdown.

import arcpy

class my_tool:
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "List Fields Demo"
        self.description = "Lists the fields in a feature class."

    def getParameterInfo(self):
        """Define the tool parameters."""
        param0 = arcpy.Parameter( # where the user will input a feature layer to convert
            displayName="Input Feature Class",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")
        return [param0]

# [Unmodified Python Toolbox template code omitted...]

    def execute(self, parameters, messages):
        """The source code of the tool."""
        desc = arcpy.Describe(parameters[0])
        src=desc.catalogPath
        arcpy.AddMessage(src)
        # 'C:/Users/my_user/Documents/scratch/test.gdb/test.gdb/data/my_fc'
        fields = arcpy.ListFields(src)
        arcpy.AddMessage(fields)

 

It's a little odd that the AddMessage shows a doubled file geodatabase name.  Even when I did some additional manipulations to remove the doubling, it isn't able to open the input feature class to list fields.

So, what's the reasonably-obvious thing that I'm missing here?

0 Kudos
1 Solution

Accepted Solutions
MobiusSnake
MVP Regular Contributor

Is your FGDB inside of a folder named "test.gdb"?  I've had this happen when unzipping FGDBs before and it causes all kinds of headaches with different tools and ArcPy functions.

View solution in original post

3 Replies
MobiusSnake
MVP Regular Contributor

Is your FGDB inside of a folder named "test.gdb"?  I've had this happen when unzipping FGDBs before and it causes all kinds of headaches with different tools and ArcPy functions.

BillMitchell
Frequent Contributor

I'm not sure how I missed this, but yes, that was the problem.  I'll add this to the list of reasons I'd rather work with Geopackages.

HaydenWelch
MVP Regular Contributor

I hate using the List* functions, they're always really finnickey.

 

You can also grab fields by using a da.SearchCursor

with arcpy.da.SearchCursor(src, '*') as cur:
    fields = cur.fields
0 Kudos