Listing data

One of the foremost tasks in a batch processing script is cataloging the available data so it can iterate through the data during processing. ArcPy has a number of functions built specifically for creating such lists.

ListFields(dataset, wild_card, field_type)

Returns a list of fields found in the input value

ListIndexes(dataset, wild_card)

Returns a list of attribute indexes found in the input value

ListDatasets(wild_card, feature_type)

Returns the datasets in the current workspace

ListFeatureClasses(wild_card, feature_type)

Returns the feature classes in the current workspace

ListFiles(wild_card)

Returns the files in the current workspace

ListRasters(wild_card, raster_type)

Returns a list of rasters found in the current workspace

ListTables(wild_card, table_type)

Returns a list of tables found in the current workspace

ListWorkspaces(wild_card, workspace_type)

Returns a list of workspaces found in the current workspace

ListVersions(sde_workspace)

Returns a list of versions the connected user has permission to use

List functions

The result of each of these functions is a Python list, which is a list of values. A list in scripting can contain any type of data, such as a string, which could be, for example, a path to a dataset, field, or row from a table. Once the list has been created with the values you want, you can loop through it in your script to work with each individual value.

Learn more about listing tools, toolboxes, and environment settings

List function parameters

The parameters of these functions are similar. A few, such as ListFields, require an input dataset value, since the items the functions are listing reside within a certain object or dataset. Other functions do not require an input dataset, because they list types of data in the current workspace that are defined in the environment settings. All functions have a wildcard parameter, which is used to restrict the objects or datasets listed by name. A wildcard defines a name filter, and all the contents in the newly created list must pass that filter. For example, you may want to list all the feature classes in a workspace that start with the letter G. The following example shows how this is done:

import arcpy
from arcpy import env

# Set the workspace. List all of the feature classes that start with 'G'
#
env.workspace = "D:/St_Johns/data.gdb"
fcs = arcpy.ListFeatureClasses("G*")

The list can also be restricted to match certain data properties, such as only polygon feature classes, integer fields, or coverage datasets. This is what the Type parameter is used for in all the functions. In the next example, the feature classes in a workspace are filtered using a wild card and a data type, so only polygon feature classes that start with the letter G are in the resulting list:

# Set the workspace. List all of the polygon feature classes that 
#   start with 'G'
#
env.workspace = "D:/St_Johns/data.gdb"
fcs = arcpy.ListFeatureClasses("G*", "polygon")

Using your list

ArcPy uses a Python list type as the returned type for all its list function results, since lists support the flexibility required for simple data access and multiple data types. A For loop is ideal for working with a Python list because it can be used to step through the list one item at a time. A For loop iterates through each item in the list. Below is an example of a For loop used to iterate through the list generated in the previous example:

# For each feature class in the list of feature classes
#
for fc in fcs: 
    # Copy the features from the workspace to a folder
    #
    arcpy.CopyFeatures_management(fc, "D:/St_Johns/Shapefiles/” + fc)

Following is another example of how to use a List function. The script is used to create raster pyramids for all rasters that are Tagged Image File Format (TIFF) images within a folder.

# Set the workspace. List all of the TIFF files
#
env.workspace= "D:/St_Johns/images"

# For each raster in the list of rasters
#
for tiff in arcpy.ListRasters("*", "TIF"): 
    # Create pyramids
    #
    arcpy.BuildPyramids_management(tiff)

A Python list provides the opportunity to use and manage the results of a list function in variety of ways. Lists are a versatile Python type and provide a number of methods (append, count, extend, index, insert, pop, remove, reverse, sort) that can be used to manipulate and extract information.

For instance, if you want to know how many feature classes you have in a workspace, you can use Python's built-in len function to provide that number.

import arcpy
from arcpy import env

env.workspace = "c:/St_Johns/Shapefiles"

fcs = arcpy.ListFeatureClasses()

# Use Python's built-in function len to reveal the number of feature classes
#   in the workspace
#
fcCount = len(fcs)
print fcCount
TipTip:

A Python list can show its contents easily. Python lists can be manipulated with a number of methods, including sort, append, and reverse.

>>> import arcpy
>>> from arcpy import env
>>> env.workspace  = "c:/data/water.gdb"
>>> fcs = arcpy.ListFeatureClasses()
>>> print fcs
[u'water_pipes', u'water_services', u'water_stations']

>>> fcs.sort(reverse=True)
>>> print fcs
[u'water_stations', u'water_services', u'water_pipes']

As lists are an ordered collection, they also permit indexing and slicing.

>>> print fcs[0]
water_stations

>>> print fcs[1:]
[u'water_services', u'water_pipes']

List function type keywords

The default behavior for all list functions is to list all supported types. A keyword is used to restrict the returned list to a specific type. The type keywords for each function are listed in the table below.

Function

Type keywords

ListDatasets

All, Feature, Coverage, RasterCatalog, CAD, VPF, TIN, Topology

ListFeatureClasses

All, Point, Label, Node, Line, Arc, Route, Polygon, Region

ListFields

All, SmallInteger, Integer, Single, Double, String, Date, OID, Geometry, BLOB

ListTables

All, dBASE, INFO

ListRasters

All, ADRG, BIL, BIP, BSQ, BMP, CADRG, CIB, ERS, GIF, GIS, GRID, STACK, IMG, JPEG, LAN, SID, SDE, TIFF, RAW, PNG, NITF

ListWorkspaces

All, Coverage, Access, SDE, Folder

Type keywords for List functions

Example: Using list functions to migrate from personal to file geodatabases

A common conversion task is the large-scale transfer of data from one format to another. The following script takes advantage of ListWorkspaces, ListFeatureClasses, ListTables, and ListDatasets to identify all personal geodatabases in a folder and converts the feature datasets, feature classes, and tables of each to file geodatabases.

import arcpy
from arcpy import env
import os

# Allow for the overwriting of file geodatabases, if they already exist
#
env.overwriteOutput = True

# Set workspace to folder containing personal geodatabases
#
env.workspace = arcpy.GetParameterAsText(0)

# Identify personal geodatabases
#
for pgdb in arcpy.ListWorkspaces("", "Access"):
    # Set workspace to current personal geodatabase
    #
    env.workspace = pgdb

    # Create file geodatabase based on personal geodatabase
    #
    fgdb = pgdb[:-4] + ".gdb"
    arcpy.CreateFileGDB_management(os.path.dirname(fgdb), os.path.basename(fgdb))

    # Identify feature classes and copy to file gdb
    #
    for fc in arcpy.ListFeatureClasses():
        print "Copying feature class " + fc + " to " + fgdb
        arcpy.Copy_management(fc, fgdb + os.sep + fc)

    # Identify tables and copy to file gdb
    #
    for table in arcpy.ListTables():
        print "Copying table " + table + " to " + fgdb
        arcpy.Copy_management(table, fgdb + os.sep + table)

    # Identify datasets and copy to file gdb
    #   Copy will include contents of datasets
    #
    for dataset in arcpy.ListDatasets():
        print "Copying dataset " + dataset + " to " + fgdb
        arcpy.Copy_management(dataset, fgdb + os.sep + dataset)

Related Topics


12/15/2011