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)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 28, 2020 at 21:58
3
  • 1
    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. Commented Jan 28, 2020 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... Commented Jan 29, 2020 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). Commented Jan 29, 2020 at 0:47

2 Answers 2

7

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.ListFeatureClasses(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)
answered Jan 28, 2020 at 22:59
0
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"
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Jan 28, 2020 at 23:53
3
  • 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. Commented Jan 29, 2020 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? Commented Jan 29, 2020 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. Commented Jan 29, 2020 at 2:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.