3

I am trying to create a new script with a hard-coded geodatabase filename, that lists the feature classes it contains to the console. I know there are several ways to do this, but I decided to to use ListFeatureClasses function. I barely started studying Python, so I know very basic stuff, but I would like to get help with this.

I tried the following code, but it does not print anything and does not even give me any error message. I opened up ArcGIS Pro, added the geodatabase to my map, and wrote this code in the built-in Python console. Is there any step I am missing or something wrong with my code?

import arcpy
import os

arcpy.env.workspace = "X:\311\Obtaining GIS Data\TaxParcels.gdb"

datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []

for ds in datasets:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        path = os.path.join(arcpy.env.workspace, ds, fc)
        print(fc)
  • Have you considered arcpy.da.Walk? resources.arcgis.com/en/help/main/10.2/index.html#//… like for FullPath, not_used, FCList in arcpy.da.Walk("X:\311\Obtaining GIS Data\TaxParcels.gdb",datatype="FeatureClass"): then for FC in FCList: to iterate, the FullPath variable has the full path to the feature class, even those in feature datasets, so path = os.path.join(FullPath,FC). I use walk because it's annoying to iterate 'standalone' feature classes, then feature datasets and the feature classes therein, also you get an option of only returning specific geometry types. – Michael Stimson Jan 28 at 23:30
  • 2
    The "\311" iis being interpreted as an "É" character. You must always either use a raw string r"your\path", escape your backslashes "your\\path" or use a forward slash "your/path instead. eg. print(r"X:\311\Obtaining...") X:\311\Obtaining... print("X:\311\Obtaining...") X:É\Obtaining... – user2856 Jan 29 at 0:04
  • What is the datasets = ['']... line for? What is the point of the path = ... line? path is never used anywhere. Don't use strings with "\" in them, unless you either escape the "\" characters with "\\" or you use a raw string (prefixed with r"...") or use normal (forward) slashes instead of backslashes. Ie, /. Python (including arcpy) is smart enough to treat forward slashes as a file path separator character even on Windows (the only commonly used OS that doesn't use / by default). – Son of a Beach Jan 29 at 0:47
5

If your fGDB actually does have feature datasets (not just top-level feature classes), you could do:

import arcpy
import os

arcpy.env.workspace = r"X:\311\Obtaining GIS Data\TaxParcels.gdb"

#  Get top-level feature classes
featureclasses = arcpy.ListFeatureClasses()

#  Get data-set feature classes
datasets = arcpy.ListDatasets()
for ds in datasets:
    featureclasses.extend(arcpy.ListDatasets(feature_dataset=ds))

for fc in featureclasses:
    print(fc)

Otherwise, if your fGDB does not have feature datasets (or if you're not interested in the feature classes within feature datasets), then it would simply be:

import arcpy
import os

arcpy.env.workspace = r"X:\311\Obtaining GIS Data\TaxParcels.gdb"
featureclasses = arcpy.ListFeatureClasses()

for fc in featureclasses:
    print(fc)
  • Sorry I realized my previous code was to "Copy shapefiles to a geodatabase." This one is, I believe, to list all feature classes in a geodatabase, including any within feature datasets, which is what I was trying to accomplish. Sorry for confusion and thanks a lot for your help. – Yoshi Jan 29 at 0:44
  • I will try your code. Thank you – Yoshi Jan 29 at 0:44
  • It worked! Thank you so much! – Yoshi Jan 29 at 19:00
1

Not sure if this is the root of your problem, but spaces in your file path can lead to unwanted (or no) results in your code. If you can, replace spaces with underscores or, better yet, camelCaseText. Also, it is a good habit to use r in your path so Python reads it as a text string:

arcpy.env.workspace = r"X:\311\Obtaining GIS Data\TaxParcels.gdb"
  • Fair point on the spaces, not so much with geodatabases but more antiquated formats like GRID and Coverage (does anyone even remember these?) spaces are a definite no-no; users who do remember these tend not to use spaces. Using a raw string (with the r in front of the string) is definitely most of the way to a solution so +1 for that. – Michael Stimson Jan 29 at 0:35
  • I went to property of the geodatabase and copied the whole path which did contain the spaces. DO wou think I still need to remove the spaces? – Yoshi Jan 29 at 0:45
  • 3
    Using spaces in directory names is like taping a "Kick Me" sign on your own back. Eventually, it's going to hurt. Best practice is to use underscore instead of spaces ("Kick_Me") and to avoid leading numeric characters in directory or file names. The reasons may be archaic, but the complications still exist. – Vince Jan 29 at 2:14

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.