Skip to main content
Added a benchmark test
Source Link
Aaron
  • 51.8k
  • 29
  • 157
  • 322

ThisThe arcpy.da.Walk() method is what I usethe way to go:

import arcpy, os

workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  datatype="FeatureClass",
                                                  type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

The main operators here are arcpy.da.Walk and len(). arcpy.da.Walk simply walks through folders and subfolders and collects files of datatype="FeatureClass" using feature_classes.append(os.path.join(dirpath, filename)). Finally, len() is used to count the number of files in the feature_classes = [] list.

EDIT:

From a benchmark standpoint and in the interest of science, it appears that arcpy.da.Walk method is significantly faster than the other approach. I tested them on a FGDB with 7 FDS and 35 FC's. Here are the results:

import arcpy, os, time

###########################################################################
# METHOD 1
start = time.clock()
workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="FeatureClass", type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

end = time.clock()
method1 = (end - start)

############################################################################
# Method 2
start = time.clock()
arcpy.env.workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"

# Get stand alone FCs
fccount = len(arcpy.ListFeatureClasses("",""))

# Get list of Feature Datasets and loop through them
fds = arcpy.ListDatasets("","")
for fd in fds:
    oldws = arcpy.env.workspace
    arcpy.env.workspace = oldws + "\\" + fd
    fccount = fccount + len(arcpy.ListFeatureClasses("",""))
    arcpy.env.workspace = oldws

print fccount

end = time.clock()
method2 = (end - start)
##########################################################################

print 'method1 completed in %s seconds; method2 completed in %s seconds' % (method1, method2)

enter image description here

This is what I use:

import arcpy, os

workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  datatype="FeatureClass",
                                                  type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

The main operators here are arcpy.da.Walk and len(). arcpy.da.Walk simply walks through folders and subfolders and collects files of datatype="FeatureClass" using feature_classes.append(os.path.join(dirpath, filename)). Finally, len() is used to count the number of files in the feature_classes = [] list.

The arcpy.da.Walk() method is the way to go:

import arcpy, os

workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  datatype="FeatureClass",
                                                  type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

The main operators here are arcpy.da.Walk and len(). arcpy.da.Walk simply walks through folders and subfolders and collects files of datatype="FeatureClass" using feature_classes.append(os.path.join(dirpath, filename)). Finally, len() is used to count the number of files in the feature_classes = [] list.

EDIT:

From a benchmark standpoint and in the interest of science, it appears that arcpy.da.Walk method is significantly faster than the other approach. I tested them on a FGDB with 7 FDS and 35 FC's. Here are the results:

import arcpy, os, time

###########################################################################
# METHOD 1
start = time.clock()
workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="FeatureClass", type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

end = time.clock()
method1 = (end - start)

############################################################################
# Method 2
start = time.clock()
arcpy.env.workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"

# Get stand alone FCs
fccount = len(arcpy.ListFeatureClasses("",""))

# Get list of Feature Datasets and loop through them
fds = arcpy.ListDatasets("","")
for fd in fds:
    oldws = arcpy.env.workspace
    arcpy.env.workspace = oldws + "\\" + fd
    fccount = fccount + len(arcpy.ListFeatureClasses("",""))
    arcpy.env.workspace = oldws

print fccount

end = time.clock()
method2 = (end - start)
##########################################################################

print 'method1 completed in %s seconds; method2 completed in %s seconds' % (method1, method2)

enter image description here

added 119 characters in body
Source Link
Aaron
  • 51.8k
  • 29
  • 157
  • 322

This is what I use:

import arcpy, os

workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  datatype="FeatureClass",
                                                  type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

The main operators here are arcpy.da.Walk and len(). arcpy.da.Walk simply walks through folders and subfolders and collects files of datatype="FeatureClass" using feature_classes.append(os.path.join(dirpath, filename)). Finally, len() is used to count the number of files in the feature_classes = [] list.

This is what I use:

import arcpy, os

workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  datatype="FeatureClass",
                                                  type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

This is what I use:

import arcpy, os

workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  datatype="FeatureClass",
                                                  type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

The main operators here are arcpy.da.Walk and len(). arcpy.da.Walk simply walks through folders and subfolders and collects files of datatype="FeatureClass" using feature_classes.append(os.path.join(dirpath, filename)). Finally, len() is used to count the number of files in the feature_classes = [] list.

Post Undeleted by Aaron
added 119 characters in body
Source Link
Aaron
  • 51.8k
  • 29
  • 157
  • 322

This is what I use:

import arcpy
from arcpy import, envos

env.workspace = arcpyr"C:\Users\OWNER\Documents\ArcGIS\Default.GetParameterAsText(0)gdb"
Dirfeature_classes = env.workspace
[]
fcsfor =dirpath, arcpy.ListFeatureClasses()
rastersdirnames, =filenames in arcpy.ListRastersda.Walk()
workspace,
length1 = len(fcs)                                                datatype="FeatureClass",
length2 = len(rasters                                                type="Any"):
    for filename in filenames:
arcpy.AddMessage("There are " + str(length1) + " Feature Classes"feature_classes.append(os.path.join(dirpath, filename))

arcpy.AddMessage(print "There are "%s +featureclasses str(length2)in +the "workspace" Rasters"% len(feature_classes)

This is what I use:

import arcpy
from arcpy import env

env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

fcs = arcpy.ListFeatureClasses()
rasters = arcpy.ListRasters()

length1 = len(fcs)
length2 = len(rasters)

arcpy.AddMessage("There are " + str(length1) + " Feature Classes")

arcpy.AddMessage("There are " + str(length2) + " Rasters")

This is what I use:

import arcpy, os

workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  datatype="FeatureClass",
                                                  type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)
Post Deleted by Aaron
Source Link
Aaron
  • 51.8k
  • 29
  • 157
  • 322
Loading