Thursday, November 29, 2012

Walk, new arcpy.da function as 10.1 SP1

Walk is a useful function that walks directories.  Though this is an easy function to create using recursion, it is now including as a core function at 10.1 SP1 for arcpy.

The help describes the function as such:
Generate data names in a catalog tree by walking the tree top-down or bottom-up. Each directory/workspace in the tree yields a tuple of three: dirpath, dirnames, and filenames. Python's os module includes an os.walk function that can be used to walk through a directory tree and find data. os.walk is file based and does not recognize database contents such as geodatabase feature classes, tables, or rasters. arcpy.da.Walk can be used to catalog data.

import arcpy
import os
workspace = "c:/data"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))