ArcPy List Datasets

6197
15
06-19-2016 07:21 PM
BrianEddy
New Contributor III

I'm an ArcPy neophyte/beginner - and this might be a stupid question, but I simply want to return a list of all the contents in a .gdb.  When I run the arcpy,listdatasets("*","Feature") function, it appears to work (no errors reported), but I do not 'see' anything.  I  used it with the 'print' function as well (using the example code provided in the help - copied below)  - but I still don't see anything.  Where does it go?  I only need to see it print to screen so I can copy/paste it....

B.

import arcpy  arcpy.env.workspace = "c:/data" 
# Print to the Interactive window all the feature datasets in the
# workspace that start with the letter

datasets = arcpy.ListDatasets("C*", "Feature") 
for dataset in datasets:

         print(dataset)

0 Kudos
15 Replies
AdrianWelsh
MVP Notable Contributor

Hi Brian,

Which Python IDE are you using?

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

I think what you want is ListFeatureClasses  ArcGIS Help (10.2, 10.2.1, and 10.2.2)

"List all feature classes in a geodatabase, including any within feature datasets" which is shown on that page, but I used the "raw" path name instead

import arcpy

import os

arcpy.env.workspace = r"c:\_temp"

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

Since you are new to python and I'm assuming the forums, I'll suggest a couple other links to get you started on the right foot.

  • Posting Code blocks in Esri GeoNet​     this shows how you can format you python code in the forum to make it easier for you and others to follow
  • Suggest that when you start using python to use the   r:"c:\youpath" formatiin.  It is a personal choice of course, but it typically is easier to read.  There are many thread on the subject, but Filenames and file paths in Python​ is a good starting point
  • in fact, Dan Patterson​ a lot of other good info on his blog The ...py... links
  • You may want to move your post to the Python​ area...if you need help with that, one of us can help.  But that would be a good place to post these types of questions since more monitor that location
  • If you find posts helpful, you can mark them as such, and then choose the once that best answers your question and mark it correct.  It help everyone when questions are closed out.

Anyway, I hope this help, but if not, reply with what issues you are having, version you are using, etc.

BrianEddy
New Contributor III

Thanks Rebecca - some good points, I'll be sure to follow....

B.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

You may be listing the wrong type of object ... see the list available

ListDatasets—Help | ArcGIS for Desktop

Also, install an IDE like pythonwin or pyscripter, the builtin IDE isn't great

In interactive mode with one shapefile in pythonwin

>>> import arcpy

>>> arcpy.env.workspace = "f:/temp"

>>> ds = arcpy.ListFeatureClasses()

>>> for d in ds:

...  print(d)

...

x.shp

>>>

>>> print(len(ds))

1

>>>

In arcmap's IDE

>>> import arcpy

>>> arcpy.env.workspace = "F:/temp"

>>> d = arcpy.ListFeatureClasses()

>>> for i in d:

...     print(i)

...    

x.shp

>>>

>>> print(len(d))

1

>>>

If nothing prints out nothing was found, so you should really check for it before you run through any loops

BrianEddy
New Contributor III

Thanks Dan. It worked.  I'm a bit confused tho' - if there are different functions for features, rasters and tables, then what is a 'Dataset'?

0 Kudos
WesMiller
Regular Contributor III

dataset

  1. [data management] Any collection of related data, usually grouped or stored together.

Esri Support GIS Dictionary

DanPatterson_Retired
MVP Esteemed Contributor

Brian... a good place to bookmark is just the documentation, then you can navigate to Arcpy to find the syntax for methods, properties and the like.

Documentation | ArcGIS for Desktop

There is one for ArcMap and for Pro which are more or less the same.  Also Geoprocessing has a link to Tool Reference should you want to incorporate the functionality of an ArcToolbox tool directly within a script.

Every link (almost) has scripting syntax and then you can go to my Py Links blog post where I keep a running list of changes to arcmap and pro as they would affect script functionality.

MicahBabinski
Regular Contributor

FWIW, I've often found arcpy's definition of "dataset" a little confusing. For example, the arcpy.Describe() "Dataset type" property will return any of these:

However, when I use arcpy.ListDatasets() on a workspace with a bunch of tables and a single feature dataset, only the feature dataset is returned. I do agree with Joshua Bixby that arcpy.da.Walk() is the most consistent way to list everything (with the added benefit of being able to "drill down" into feature datasets).

So to summarize, my concept (and please correct me if I am mistaken!) is that a "dataset" in ArcGIS is any of the things listed in the graphic above.

Micah

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In my opinion, the documentation screenshot you posted is a documentation bug, or the reflection of a software bug in documentation.  The behavior you see with arcpy.ListDatasets() is expected, and I would argue correct.  Datasets are container "data sets," e.g., feature dataset, raster dataset, mosaic dataset, parcel fabric, and others.  A feature class isn't a dataset, but in a general sense it is a "data set."  There is an Abbott and Costello skit in there somewhere.

Where the bug, as I see it, comes into play is with the ArcPy Describe object.  When describing a feature class, the datasetType property and dataType property both return the same value.  That same is true when describing feature datasets.  Interestingly enough, the same is also true for topologies and every other dataset I have tried.  Which raises the question, why have two properties instead of just one?  One would think a non-dataset wouldn't support the datasetType property, but it does, and that causes confusion as to what exactly a dataset is.

I won't even get started on "Geo."  I have been pushing for them to either clearly define it or remove it because its current implementation makes no sense at all.