I'm trying to run slope analysis in ArcGIS. I have written an ArcPy script that converted feature layers to raster files. Now I have a list of rasters (each with a unique name) in the table of contents and I need to run slope analysis to create new rasters for each one. I've written a script that works, but only on the raster at the bottom of the list in the table of contents.

Here is my script:

import arcpy
import os
from arcpy import env
from arcpy.sa import *
# Set the current workspace
in_workspace = r"E:\DEM Files"
out_workspace = r"E:\DEM Files"
MXD = arcpy.mapping.MapDocument("Current")
DF  = arcpy.mapping.ListDataFrames(MXD, "Layers")[0]
rasters = arcpy.ListRasters("*", "TIFF")
for rasters in DF:                      
        # Set local variables
        inRaster = rasters
        outMeasurement = "PERCENT_RISE"
        zFactor = 1
        # Execute Slope
        outSlope = Slope(inRaster, outMeasurement, zFactor)

del MXD`
  • I'm thinking that I need to change what I currently have for "inRaster = rasters" but I'm not sure what to use. I can't enter each individual raster name.
    – amoore9984
    Oct 15, 2014 at 20:12

2 Answers 2


ListRasters will list the rasters in your workspace. ListLayers will list the rasters in a map document.

rasters = arcpy.mapping.ListLayers(MXD, "", DF)
for raster in rasters:
    #do work

Use the wildcard argument to limit your results.

  • Now I'm receiving the message: "AttributeError: LayerObject: Get attribute layers does not exist."
    – amoore9984
    Oct 15, 2014 at 20:43
  • @amoore9984 I think it will be helpful if you can include the full error message (e.g. line number) being received against the precise code that you are using, by editing that into your question.
    – PolyGeo
    Oct 15, 2014 at 21:03
  • I'm now receiving the message: "RuntimeError: Object: Error in executing tool" and tracebacks for line 5523 (in Slope), line 47 (in Swapper result), in 5518 (in wrapper), and in line 498 (in lambda).
    – amoore9984
    Oct 15, 2014 at 21:11
  • it looks like you want to use either ListRasters or ListLayers, not both. At least one current issue will be that your rasters variable is overwritten: rasters = arcpy.ListRasters("*", "TIFF") is immediately replaced by layers in DF in the next line: for rasters in DF: Oct 16, 2014 at 2:25

EDIT : I assume that you have a correct list of rasters, you can check this using


you need to save your raster at each iteration in order to persist it. Note that it is not necessary to use the mxd.

outMeasurement = "PERCENT_RISE"
zFactor = 1
arcpy.env.workspace = in_workspace #note that it is recommended to avoid spaces in your workspace names
for raster in rasters:
    outSlope = Slope(raster, outMeasurement, zFactor)

you can also set the output name if you want (iterating on the rasters with ListRasters)


or if you use an iterator of Layer objects (iterating with ListLayers)

  • I added outSlope.save() to the end of the script, but it still only runs on the raster at the bottom of the table of contents and then stops without continuing to the next raster in the list.
    – amoore9984
    Oct 15, 2014 at 20:11
  • my second option will create unique output names base on the input name.
    – radouxju
    Oct 16, 2014 at 5:21
  • I tried to set the output name, but I received this message: Runtime error Traceback (most recent call last): File "<string>", line 18, in <module> AttributeError: 'Layer' object has no attribute 'catalogPath'
    – amoore9984
    Oct 29, 2014 at 17:51
  • My slope script seems to be working now, so the problem is saving the new slope layers and not overwriting them each time.
    – amoore9984
    Oct 29, 2014 at 17:53
  • try dataSource instead of catalogPath if you iterate on Layers (see my edit).
    – radouxju
    Oct 29, 2014 at 18:49

Your Answer

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

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