Creating workflows using the Python window
The Python window supports the execution of any of hundreds of geoprocessing tools through the ArcPy site package. You can execute a single tool or run multiple tools in a specific order. The Python window also provides access through ArcPy to many functions and classes that support more complex Python workflows.
Among the functions that support Python workflows are methods to make lists of certain data types, retrieve a dataset's properties, validate a table name before adding it to a geodatabase, or perform one of many other useful scripting functions.
The following code sample uses the ListFeatureClasses function to generate a list of all the feature classes in a workspace; then the GetCount tool is used to print the number of features for each feature class in that list.
>>> arcpy.env.workspace = "c:/data" >>> for fc in arcpy.ListFeatureClasses(): ... print fc, arcpy.GetCount_management(fc)
For example, it is time consuming and impractical to specify all the detailed properties of a spatial reference in Python. By using the SpatialReference class, the properties of a SpatialReference object can be quickly completed and the object used as input to a geoprocessing tool. In the following code sample, a SpatialReference object is populated with properties of the North American Equidistant Conic projection from a projection file; then the object is used with the CreateFeatureClass tool to create a new feature class with that specified spatial reference.
import arcpy inputWorkspace = "c:/temp" outputName = "rivers.shp" spatialRef = arcpy.SpatialReference("c:/data/North America Equidistant Conic.prj") arcpy.CreateFeatureClass_management(inputWorkspace, outputName, "POLYLINE", "", "", "", spatialRef)
Functions are small blocks of code that perform a specific task, which can then be incorporated into more detailed workflows. Once you have created a function, it can be used repeatedly, saving you the effort of having to rewrite the same tasks over and over.
def listFieldNames(table, wildcard=None, fieldtype=None): fields = arcpy.ListFields(table, wildcard, fieldtype) nameList =  for field in fields: nameList.append(field.name) return nameList
In Python, functions are defined using Python's def keyword, followed by the function name and list of parameters. The above function, listFieldNames, is a short function that returns a list of field names from a table or feature class. The return statement is used to return a value from the function.
>>> fieldNames = listFieldNames("c:/data/water.gdb/water_pipes")
However, if you have to create functions on a routine basis to get your work done, you should consider writing a script tool. It is a natural progression from writing short snippets of code to writing your own functions to writing geoprocessing script tools. Learn more about creating your own script tools.