[GIS] Error 000875 when using setting workspace with user input for slope spatial analyst tool

arcpyerror-000875

Why does the slope tool run fine until I use a user input to set the workspace?

Background: Below you will see the code that works fine at the top preceded by ##. When I change the workspace to be defined by a user input as shown beneath that, it no longer works. This is part of a larger script. In the larger script the workspace is set with the user input and it works fine with mosaic to new raster, project raster, and clip raster, but then it errors on the slope tool. I am now testing in the shorter script shown below so I don't have to wait for it to run each time.

I have tried changing drives, shortening the path, I switched everything from Esri Grid to tif because of the naming limitations, I tried a shorter name for the output raster.

##import arcpy
##from arcpy.sa import *
##arcpy.env.workspace = r'E:\My_Workspace\Processed_Products'
##
##
##
##if arcpy.CheckExtension('spatial') == 'Available':
##    arcpy.CheckOutExtension ('spatial')
##    outraster = Slope('DEM_Clip.tif',"PERCENT_RISE",1)
##    outraster.save('DEM_Slope1.tif')
##    print "The slope dataset has been generated"
##else:
##    print 'spatial analyst not available'

import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = True

ws = raw_input('Please paste the workspace path here. ')
arcpy.env.workspace = "r" + "'" + ws + "'"

print " ws is " + ws

DEM_Clip = ws + "\\Processed_Products\\DEM_Clip.tif"

print "The file below has been created and it is in the Processed_Products folder"
print DEM_Clip

if arcpy.CheckExtension('spatial') == 'Available':
    arcpy.CheckOutExtension ('spatial')
    outraster = Slope(DEM_Clip,"PERCENT_RISE",1)
    outraster.save('DEM_Slope1.tif')
    print "The slope dataset has been generated"
else:
    print 'spatial analyst not available'

arcpy.CheckInExtension("spatial")

Output:

Please paste the workspace path here. E:\My_Workspace
 ws is E:\My_Workspace
The file below has been created and it is in the Processed_Products folder
E:\My_Workspace\Processed_Products\DEM_Clip.tif

Traceback (most recent call last):
  File "G:\GIS_Programming\488_Data\Lecture_10_Scripts\Lecture_10_4.py", line 33, in <module>
    outraster = Slope(DEM_Clip,"PERCENT_RISE",1)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\sa\Functions.py", line 6054, in Slope
    z_factor)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\sa\Functions.py", line 6049, in Wrapper
    z_factor)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 000875: Output raster: r'E:\My_Workspace'\Slope_DEM_Cl1's workspace is an invalid output workspace.
ERROR 000581: Invalid parameters.
Failed to execute (Slope).

Best Answer

Your problem is in setting your arcpy.env.workspace. Your code says

arcpy.env.workspace = "r" + "'" + ws + "'"

which is producing a path of "r'E:\My_Workspace'" which is a string that contains quoted text. You want it to instead look like r'E:\My_Workspace' (note no extra quote marks)

Your workspace should just be

arcpy.env.workspace = ws
Related Question