I would like to identify within a particular folder the .lyr files that have broken links. Some of these files are within multiple folders but all fall within one folder. My programming knowledge is fairly novice so I may use confusing or incorrect terminology.
I have been following the Q&A below, and have got the script to successfully identify .mxd's in a test folder only containing one .mxd file containing a broken .lyr file. When trying the .lyr script given in the same thread on a test folder containing a broken .lyr file both IDLE and Arc Desktop (10.2) crash.
Use arcpy.mapping to list broken data layers?
successful broken .lyr in mxd script:
import arcpy, os
path = r"M_XXX"
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)
brknList = arcpy.mapping.ListBrokenDataSources(mxd)
for brknItem in brknList:
print "MXD: " + fullPath
print "\t" + brknItem.name
crash .lyr script:
import arcpy, os
path = r"M_XXX"
for root, dirs, files in os.walk(path):
for fileName in files:
basename, extension = os.path.splitext(fileName)
if extension == ".lyr":
fullPath = os.path.join(root, fileName)
mxd = arcpy.mapping.MapDocument(fullPath)
brknList = arcpy.mapping.ListBrokenDataSources(mxd)
for brknItem in brknList:
print "LYR: " + fullPath
print "\t" + brknItem.name
-
1Welcome to GIS StackExchange! When you have a chance, please take the tour. Please also edit your post to include any error details. Another way you can troubleshoot strange errors is to add print statements to determine the last line that successfully printed.SMiller– SMiller2018年11月05日 16:39:06 +00:00Commented Nov 5, 2018 at 16:39
1 Answer 1
Use a describe object to identify a layer's feature class. Then use Exists to determine if the feature class exists. If it doesn't the link will be broken.
import arcpy, os
path = r"M_XXX"
for root, dirs, files in os.walk(path):
for fileName in files:
basename, extension = os.path.splitext(fileName)
if extension == ".lyr":
fil = os.path.join (root, fileName)
desc = arcpy.Describe (fil)
fc = desc.featureClass.catalogPath
if not arcpy.Exists (fc):
print "broken:"
print fil
-
returns the following: Traceback (most recent call last): File "M:", line 9, in <module> fc = desc.featureClass.catalogPath AttributeError: DescribeData: Method featureClass does not existFRMMO– FRMMO2018年11月06日 10:17:12 +00:00Commented Nov 6, 2018 at 10:17
Explore related questions
See similar questions with these tags.