0

Trying to find a way to use Python or SQL to loop through geodatabases and print the feature classes. I'm not sure what the difference between while and for loops are; I'm brand new to scripting. A point in the right direction would be great.

  • Welcome to GIS SE! We're a little different from other sites. We're a Q&A site, not a discussion forum. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. Please check out our short tour for more about how the site works. Thanks. – PolyGeo Aug 27 '19 at 23:51
  • Questions about the a Python language itself, including how to use the looping constructs of the language, are off-topic here in GIS SE, where the questions should be GIS-centric. The ArcPy documentation includes examples of using each function. The are also thousands of question/answers here which include code; a google of arcpy.FunctionName is likely to return a GIS SE link. – Vince Aug 28 '19 at 0:06
2

If you're asking about Esri geodatabases, arcpy.ListFeatureClasses does exactly this given an input workspace (e.g. geodatabase connection).

The code (Python 3) from their example:

import arcpy
import os

arcpy.env.workspace = "c:/base/gdb.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(path)

A while loop is used because we don't know how many items there are so the loop continues until it reaches the last item. A for loop is used when you want to explicitly determine how many times the loop will be executed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.