0

I am trying to remove anything in a map that has "DI" in the source path.

I have a test mxd with one df and 2 lyr's. The lyr data sources are:

C:\Users\Corey.Fields\Python\Testing\DI_Remove\Fake_DI_Wells.shp S:\Departments\GIS\ARCUSERS\PERSONAL_FOLDERS\CFields\Test.gdb\Fake_DI_Test

My code is:

import arcpy
mxd = arcpy.mapping.MapDocument("C:\Users\Corey.Fields\Python\Testing\DI_Remove\Untitled.mxd")
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        if lyr.dataSource == "*DI*":
            arcpy.mapping.RemoveLayer(df, lyr)
mxd.save()
del mxd

The script is running but not removing the layers.

What do I have wrong?

2 Answers 2

2

You need to evaluate whether "DI" is in the data source.

if "DI" in lyr.dataSource:
0
1

You cannot use "*", rather use .find method to evaluate:

for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        found = lyr.dataSource.find('DI')
        if found >-1:
            arcpy.mapping.RemoveLayer(df, lyr)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.