Just check for the name of the layers you want to turn off:
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, '')[0]
layersOff = ['layer2', 'layer3', 'layer5'] # Names of the layers you want turned off
list_layers = arcpy.mapping.ListLayers(mxd, '', df)
for layer in list_layers:
layer.visible = True
if layer.name in layersOff:
layer.visible = False
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
In this example, any layer names that match a name in layersOff
are turned off, everything else is turned on.
Your list_layers
was actually trying to get a list of Data Frames not layers, so I've added a arcpy.ListDataFrames()
and corrected your arcpy.ListLayers()
.