Checking for the existence of data
To check for the existence of data in a script, use the Exists function.
Tests for the existence of feature classes, tables, datasets, shapefiles, workspaces, layers, and other files in the current workspace at the time of execution. The function returns a Boolean indicating whether the element exists.
When checking for the existence of geographic data, you must use the Exists function, since it recognizes catalog paths. A catalog path is a pathname that only ArcGIS recognizes. For example: D:\Data\Final\Infrastructure.gdb\EastValley\powerlines refers to the powerlines feature class found in the EastValley feature dataset in the file geodatabase Infrastructure. This is not a valid system path as far as the Windows operating system is concerned, since Infrastructure.gdb (a folder) does not contain a file named Infrastructure. In short, Windows doesn't know about feature datasets or feature classes, so you cannot use Python existence functions like os.path.exists. Of course, everything in ArcGIS knows how to deal with catalog paths. UNC (Universal Naming Convention) paths can also be used.
import arcpy from arcpy import env env.workspace = "d:/St_Johns/data.gdb" fc = "roads" # Clip a roads feature class if it exists # if arcpy.Exists(fc): arcpy.Clip_analysis(fc,"urban_area","urban_roads")
The Exists function honors the geoprocessing workspace environment allowing you to just specify the base name.
If the data resides in an enterprise geodatabase, the name must be fully qualified.
import arcpy from arcpy import env env.workspace = "Database Connections/Bluestar.sde" fc = "ORASPATIAL.Rivers" # Confirm that the feature class exists # if arcpy.Exists(fc): print "Verified %s exists" % fc
In scripting, the default behavior for all tools is to not overwrite any output that already exists. This behavior can be changed by setting the overwriteOutput property to True (arcpy.env.overwriteOutput = True). Attempting to overwrite when the overwriteOutput is False will cause a tool to fail.