Finding additional Python examples
There are many examples of using Python to coordinate and execute geoprocessing tools available in the scripting help. Many of these examples focus on the use of specific methods or properties of the geoprocessor and are intended to be concise and easy to follow. More detailed and sometimes complicated examples of how to use Python to solve problems can be found as tools in ArcGIS. While most tools delivered with ArcGIS are written in C++ and delivered in a binary form, some are written in Python or ModelBuilder. Some tools execute batch operations, such as loading data into a geodatabase or projecting a set of feature classes, which is perfectly suited for scripting as the solution. The underlying script can be opened at any time to see what happens when the tool is run. Use this capability to learn how to use arcpy in a number of situations, such as creating multiple ring buffers or building pyramids for a set of rasters. Script tools have a unique icon so you can easily find tools that use scripts to execute. Use the edit option in a tool's context menu to open and examine the underlying script.
These scripts cannot be edited, as they are read-only, but you can copy the tool to a custom toolbox and its underlying script to another folder so you can make edits if desired.
The help for each geoprocessing tool also contains a section specific to scripting, with a description of each parameter and an example of how to use the tool in a Python script.
Exporting models to scripts
Any model created in ModelBuilder can be exported to a script. This can be one technique for furthering your model as well as seeing how tools and environments are used in a scripting environment.
Take the following points under consideration when exporting a model to a script:
- If you expect to be overwriting data, set the overwriteOutput property to True.
- If your model used layers or table views that weren't created within the original model, those layers or table views will have to created in the script using tools such as MakeFeatureLayer and MakeTableView.
- Data elements from your model are converted directly into variables in the exported script. Avoid naming data elements that will be incompatible in Python (for example, class, global, return). For a full list of reserved keywords, use Python's keyword module.
import keyword print keyword.kwlist
Additional scripting samples
For information on a particular geoprocessing tool, see the online geoprocessing command reference or click the Help button on a geoprocessing tool's dialog box. An explanation of each parameter and example scripting code is provided.
User-created Python scripts can be found on the Geoprocessing Resource Center Tool Gallery.
Python-specific help can be found on the Python Web site.