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("{0}, {1}".format(fc, arcpy.GetCount_management(fc)))
Classes such as spatial references, field maps, and fields and indexes can be used to create objects that can be used with other geoprocessing tools or functions.
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; then the object is used with the Create Feature Class tool to create a new feature class with that specified spatial reference.
import arcpy
inputWorkspace = "c:/temp"
outputName = "rivers.shp"
spatialRef = arcpy.SpatialReference("North America Equidistant Conic")
arcpy.CreateFeatureclass_management(inputWorkspace, outputName, "POLYLINE", "", "", "", spatialRef)
Creating functions
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.