Skip to main content

Checking for the existence of data

To check for the existence of data in a script, use the Exists function. The Exists function tests for the existence of feature classes, tables, datasets, shapefiles, workspaces, layers, and other files in the current workspace. The function returns a Boolean indicating whether the element exists.

Use the Exists function to test for the existence of ArcGIS data. Exists works with catalog paths, which ArcGIS understands but the operating system does not. Catalog paths can point to items such as feature datasets and feature classes inside a geodatabase. For example, d:\data\infrastructure.gdb\powerlines is a valid ArcGIS path, even though Windows cannot resolve it. Windows treats infrastructure.gdb as a folder and does not recognize the geodatabase structure within it, so Python file system functions like os.path.exists will not work.

Because ArcGIS understands catalog paths, the Exists function should be used instead. UNC paths are also supported.

import arcpy

arcpy.env.workspace = r"d:\St_Johns\data.gdb"
fc = "roads"

# Clip a roads feature class if it exists
if arcpy.Exists(fc):
   arcpy.analysis.Clip(fc, "urban_area", "urban_roads")
Tip:

The Exists function honors the Current 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

arcpy.env.workspace = r"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.