HOW TO

Print a list of broken layers in ArcGIS Desktop using Python

Last Published: September 11, 2023

Summary

When a data source cannot be found for a particular layer in an ArcGIS Desktop project or map document, the layer fails to draw and a red exclamation point appears next to the item in the Table of Contents showing that it is considered broken. The data source can be broken if references have been moved, renamed, deleted, or become inaccessible for reasons such as folder permissions. Python can be used to efficiently identify broken layers.

Procedure

There are two ways to identify broken layers, either from a map document (.mxd) or a project (.aprx). Use the ListBrokenDataSource() Python function to check for broken layers in map documents in ArcMap, and the isBroken property of the Layer class to list broken layers in an ArcGIS Pro project.

To list broken layers in map documents in ArcMap:

  1. Import the necessary modules.
import arcpy, os
  1. Specify the path to the .mxd file.
path = r"<path to folder>"
  1. Create a loop to search for files in the provided path.
for root, dirs, files in os.walk(path): 
    for fileName in files: 
        basename, extension = os.path.splitext(fileName)
  1. In the same loop, create a selection statement to identify the .mxd files, then create a full path including the names of the .mxd files and specify the broken list into a parameter.
if extension == ".mxd":  
        fullPath = os.path.join(root, fileName)  
        mxd = arcpy.mapping.MapDocument(fullPath)  
        arcpy.AddMessage(mxd)  
        brknMXD = arcpy.mapping.ListBrokenDataSources(mxd)
  1. Create a loop to print all the broken items in the same loop.
for brknItem in brknMXD:
        print "\t" + brknItem.name
del mxd

The following is a sample of the full script:

import arcpy, os  
path = r"C:\\Test"  
 
for root, dirs, files in os.walk(path):  
    for fileName in files:  
        basename, extension = os.path.splitext(fileName)  
        if extension == ".mxd":  
            fullPath = os.path.join(root, fileName)  
            mxd = arcpy.mapping.MapDocument(fullPath)  
            arcpy.AddMessage(mxd)  
            brknMXD = arcpy.mapping.ListBrokenDataSources(mxd)  
            for brknItem in brknMXD:
            	print "\t" + brknItem.name
	    del mxd

To list broken layers in an ArcGIS Pro project:
  1. Import the necessary modules.
import arcpy, os
  1. Specify the path to the .mxd file.
path = r"C:\\Test"
  1. Create a loop to search for files in the provided path and combine the item names in the path.
for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
  1. Create a selection statement to identify the project file (.aprx) and list the broken layers.
if os.path.isfile(fullPath):
    basename, extension = os.path.splitext(fullPath)
    if extension == ".aprx":
        aprx = arcpy.mp.ArcGISProject(fullPath)
        print("APRX: " + fileName)
        for m in aprx.listMaps():
           print()
        for lyr in m.listLayers():
            if lyr.isBroken:
                print("(BROKEN) " + lyr.name)
            else:
                print("  " + lyr.name)

The following is a sample of the full script:

import os
import arcpy

path = r"C:\\Test"
for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
    if os.path.isfile(fullPath):
        basename, extension = os.path.splitext(fullPath)
        if extension == ".aprx":
            aprx = arcpy.mp.ArcGISProject(fullPath)
            print("APRX: " + fileName)
            for m in aprx.listMaps():
                print()
            for lyr in m.listLayers():
                if lyr.isBroken:
                    print("(BROKEN) " + lyr.name)
                else:
                    print("  " + lyr.name)
del aprx

Article ID: 000020970

Software:
  • ArcMap
  • ArcGIS Pro

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options