2

I have created a python script in ArcGIS 10.1. My first parameter type is a 'Feature Layer' set to as a multivalue input. I was running my script quite well off of data from one of my drives (my Y:drive), but then I tried use some data from another drive (my Z:drive) and am receiving an error:

ERROR 000732: Input Features: Dataset 'Z:\DATA FOR 2009\Base.gdb\CREEKS_UTM' does not exist or is not supported

I set a print message to print out the parameters for me and this is what i got:

Y:\2012_data\INFRASTRUCTURE.gdb\Buildings;'Z:\DATA FOR 2009\Base.gdb\CREEKS_UTM';'Z:\DATA FOR 2009\Base.gdb\LAKES_UTM'

The only anomaly I see is that the 2 feature classes from the Z:drive is being wrapped in single quotes when it returns the value, but the Y:drive doesn't. I have no idea why this is, but I'm thinking that is the issue I am having.

My script basically just reads in the parameters and clips to the extent of a raster file I have. The reason for the conditional statement in the loop is because the user may use a layer file from ArcMap or they may navigate to a feature class/shapefile and use that as an input. If they are using the layer in the map, then it reads it in as just the layer name (i.e 'Buildings'). If they navigate, then the parameter reads the whole file path and name (i.e Y:\2012_data\INFRASTRUCTURE.gdb\Buildings), so I need to parse it to use the base file name for the output parameter in the clip tool.

The files from the Z:drive is not being satisfied in the if/else statement. When I test against os.path.isabs it ops to the else part of the statement, so it must be having issues with the quotes in the isabs test.

Any suggestions are welcome.

My script is as follows:

import arcpy, os

inLines = arcpy.GetParameterAsText(0)
inRaster = arcpy.GetParameterAsText(1)
outRaster = arcpy.GetParameterAsText(2)

pnt_array = arcpy.Array()
extent = arcpy.Raster(inRaster).extent
pnt_array.add(extent.lowerLeft)
pnt_array.add(extent.lowerRight)
pnt_array.add(extent.upperRight)
pnt_array.add(extent.upperLeft)

poly = arcpy.Polygon(pnt_array)

for shp in inLines.split(';'):
    arcpy.AddMessage(shp)
    arcpy.AddMessage(os.path.isabs(shp))
    if os.path.isabs(shp) == True:
        arcpy.AddMessage('if')
        (filename, ext) = os.path.splitext(os.path.basename(shp))

        arcpy.Clip_analysis(shp, poly, outRaster + "\\" + str(filename) + "_Clip")        

    else:
        arcpy.AddMessage('else')
        arcpy.Clip_analysis(shp, poly, outRaster + "\\" + str(shp) + "_Clip")
4
  • 2
    Have you tried removing spaces from your file path. Sometimes ArcGIS (especially with Python) does not like spaces or other special characters in file paths. The spaces are the reason the Z: files is in single quotes.
    – Brian
    Apr 8, 2013 at 22:27
  • You know, that's probably exatly what is going on. There is nothing I can do about changing the spaces in the path because it would affect the datasource to hundreds of maps. Is there any way to test the path for this and force it to pass the isabs test?
    – Mike
    Apr 8, 2013 at 22:36
  • 1
    You could just do the os.path.splitext(os.path.basename(shp)) to everything. If there is not extension than splitext will still return the filename. If there is not path (just a filename) than basename will still return the filename.
    – Brian
    Apr 8, 2013 at 22:52
  • Thanks Brian. This got me going in the right direction, but I found a pretty simple way to do this, after a long day of frustration. I'll post my answer :)
    – Mike
    Apr 8, 2013 at 23:37

2 Answers 2

3

ArcPy adds single quotes around parameters that contain spaces. You can use filename.strip("'") to get rid of them. This is safe to use even if there are no single quotes in the string:

>>> filename1=r"'Z:\DATA FOR 2009\Base.gdb\CREEKS_UTM'"
>>> filename2=r"Z:\DATA_FOR_2009\Base.gdb\CREEKS_UTM"
>>> print filename1.strip("'")
Z:\DATA FOR 2009\Base.gdb\CREEKS_UTM
>>> print filename2.strip("'")
Z:\DATA_FOR_2009\Base.gdb\CREEKS_UTM

So your code could look something like:

shp=shp.strip("'")
if os.path.isabs(shp) == True:
    arcpy.AddMessage('if')
    (filename, ext) = os.path.splitext(os.path.basename(shp))

    arcpy.Clip_analysis(shp, poly, outRaster + "\\" + str(filename) + "_Clip")
else:
    arcpy.AddMessage('else')
    arcpy.Clip_analysis(shp, poly, outRaster + "\\" + str(shp) + "_Clip")

Note that the isabs test is also not neccesary:

>>> filename1="'Z:\DATA FOR 2009\CREEKS_UTM.shp'"
>>> filename2="CREEKS_UTM"
>>> (filename, ext) = os.path.splitext(os.path.basename(filename1.strip("'")))
>>> print filename
CREEKS_UTM 
>>> (filename, ext) = os.path.splitext(os.path.basename(filename2.strip("'")))
>>> print filename
CREEKS_UTM 

So your code could look something like this:

(filename, ext) = os.path.splitext(os.path.basename(shp.strip("'")))
arcpy.Clip_analysis(shp, poly, os.path.join(outRaster,shp) + "_Clip")
3
  • Two quick tips: (1) if <condition> == True: is redundant, so the == True part should be removed; (2) use os.path.join(outRaster, filename) to construct file paths
    – Mike T
    Apr 9, 2013 at 5:57
  • I know that. It's the OP's code (gis.stackexchange.com/a/57508/2856). I used os.path.join. Refer to the final code suggestion.
    – user2856
    Apr 9, 2013 at 6:23
  • Thanks for taking the time Luke. I also appreciate showing the more efficient way of testing the strings. I'm going try that in my script. Cheers.
    – Mike
    Apr 9, 2013 at 15:20
1

I found a solution to this. I added in an elif and tested for os.path.isabs after removing the quotes using shp[1:-1]. Make sure to add shp[1:-1] as the input parameter in arcpy.Clip_analysis()

if os.path.isabs(shp) == True:
    arcpy.AddMessage('if')
    (filename, ext) = os.path.splitext(os.path.basename(shp))

    arcpy.Clip_analysis(shp, poly, outRaster + "\\" + str(filename) + "_Clip")

elif os.path.isabs(shp[1:-1]) == True:
    arcpy.AddMessage('elif')
    (filename, ext) = os.path.splitext(os.path.basename(shp))
    arcpy.Clip_analysis(shp[1:-1], poly, outRaster + "\\" + str(filename[0:-1]) + "_Clip")

else:
    arcpy.AddMessage('else')
    arcpy.Clip_analysis(shp, poly, outRaster + "\\" + str(shp) + "_Clip")

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.