To check for the existence of data in a script, use the Exists function.
Exists(dataset) | 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, use the Exists function, since it recognizes catalog paths. A catalog path is a path name 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. Universal Naming Convention (UNC) paths can also be used.
import arcpy
arcpy.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")
If the data resides in an enterprise geodatabase, the name must be fully qualified.
import arcpy
arcpy.env.workspace = "Database Connections/Bluestar.sde"
fc = "ORASPATIAL.Rivers"
# Confirm that the feature class exists
#
if arcpy.Exists(fc):
print("Verified {} exists".format(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, causes a tool to fail.